

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>quippy.castep &mdash; quippy 7ea04c5+ documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "math|output_area", "processEscapes": true, "ignoreClass": "document", "inlineMath": [["$", "$"], ["\\(", "\\)"]]}})</script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> quippy
          

          
            
            <img src="../../_static/hybrid.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                7ea04c5+
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
    
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to <code class="docutils literal notranslate"><span class="pre">quippy</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation of QUIP and quippy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Tutorials/index.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quippy.html">Quippy library reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../visualisation.html">Visualisation Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fortran_wrapper.html">Appendix: wrapping Fortran 90 Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../teach_sparse.html">Command line options of the teach_sparse main program</a></li>
</ul>

            
          
    <p></p>
    <ul>
        <li><a href="../../genindex.html">Index</a></li>
        <li><a href="../../py-modindex.html">Module Index</a></li>
        <li><a href="../../search.html">Search Page</a></li>
        <li><a href="../index.html">Module Source Code</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">quippy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>quippy.castep</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for quippy.castep</h1><div class="highlight"><pre>
<span></span><span class="c1"># HND XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   libAtoms+QUIP: atomistic simulation library</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   Portions of this code were written by</span>
<span class="c1"># HND X     Albert Bartok-Partay, Silvia Cereda, Gabor Csanyi, James Kermode,</span>
<span class="c1"># HND X     Ivan Solt, Wojciech Szlachta, Csilla Varnai, Steven Winfield.</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   Copyright 2006-2010.</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   Not for distribution</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   Portions of this code were written by Noam Bernstein as part of</span>
<span class="c1"># HND X   his employment for the U.S. Government, and are not subject</span>
<span class="c1"># HND X   to copyright in the USA.</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   When using this software, please cite the following reference:</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X   http://www.libatoms.org</span>
<span class="c1"># HND X</span>
<span class="c1"># HND X  Additional contributions by</span>
<span class="c1"># HND X    Alessio Comisso, Chiara Gattinoni, and Gianpietro Moras</span>
<span class="c1"># HND X</span>
<span class="c1"># HND XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>

<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">string</span><span class="o">,</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">operator</span><span class="o">,</span> <span class="nn">itertools</span><span class="o">,</span> <span class="nn">logging</span><span class="o">,</span> <span class="nn">glob</span><span class="o">,</span> <span class="nn">re</span><span class="o">,</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">quippy.atoms</span> <span class="k">import</span> <span class="n">Atoms</span><span class="p">,</span> <span class="n">make_lattice</span><span class="p">,</span> <span class="n">get_lattice_params</span>
<span class="kn">from</span> <span class="nn">quippy.io</span> <span class="k">import</span> <span class="n">AtomsReaders</span><span class="p">,</span> <span class="n">AtomsWriters</span><span class="p">,</span> <span class="n">atoms_reader</span>
<span class="kn">from</span> <span class="nn">quippy.dictionary</span> <span class="k">import</span> <span class="n">Dictionary</span>
<span class="kn">from</span> <span class="nn">quippy.units</span> <span class="k">import</span> <span class="n">AU_FS</span><span class="p">,</span> <span class="n">HARTREE</span><span class="p">,</span> <span class="n">BOHR</span><span class="p">,</span> <span class="n">BOLTZMANN_K</span><span class="p">,</span> <span class="n">EV_A3_IN_GPA</span><span class="p">,</span> <span class="n">DEBYE</span>
<span class="kn">from</span> <span class="nn">quippy.periodictable</span> <span class="k">import</span> <span class="n">atomic_number</span>

<span class="kn">from</span> <span class="nn">ordereddict</span> <span class="k">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">farray</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">pi</span>
<span class="kn">import</span> <span class="nn">xml.dom.minidom</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;CastepCell&#39;</span><span class="p">,</span> <span class="s1">&#39;CastepParam&#39;</span><span class="p">,</span> <span class="s1">&#39;CastepPotential&#39;</span><span class="p">,</span>
           <span class="s1">&#39;check_pspots&#39;</span><span class="p">,</span> <span class="s1">&#39;run_castep&#39;</span><span class="p">,</span> <span class="s1">&#39;read_formatted_potential&#39;</span><span class="p">,</span>
           <span class="s1">&#39;read_formatted_density&#39;</span><span class="p">]</span>
           
<span class="n">castep_units</span> <span class="o">=</span> <span class="p">{</span>
   <span class="s1">&#39;ang&#39;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
   <span class="s1">&#39;bohr&#39;</span> <span class="p">:</span> <span class="n">BOHR</span>
   <span class="p">}</span>

<span class="n">castep_value_map</span> <span class="o">=</span> <span class="p">{</span>
   <span class="s1">&#39;T&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
   <span class="s1">&#39;true&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
   <span class="s1">&#39;F&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
   <span class="s1">&#39;false&#39;</span><span class="p">:</span> <span class="kc">False</span>
   <span class="p">}</span>

<span class="n">castep_output_map</span> <span class="o">=</span> <span class="p">{</span>
   <span class="kc">True</span><span class="p">:</span> <span class="s1">&#39;true&#39;</span><span class="p">,</span>
   <span class="kc">False</span><span class="p">:</span> <span class="s1">&#39;false&#39;</span>
   <span class="p">}</span>

<span class="c1"># Valid CELL and PARAMETER keywords. Generated using get_valid_keywords() in this module with CASTEP 8.0.</span>

<span class="n">valid_cell_keywords</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;lattice_cart&#39;</span><span class="p">,</span> <span class="s1">&#39;lattice_abc&#39;</span><span class="p">,</span> <span class="s1">&#39;positions_frac&#39;</span><span class="p">,</span> <span class="s1">&#39;positions_abs&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;symmetry_generate&#39;</span><span class="p">,</span> <span class="s1">&#39;symmetry_ops&#39;</span><span class="p">,</span> <span class="s1">&#39;symmetry_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;ionic_constraints&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;fix_com&#39;</span><span class="p">,</span> <span class="s1">&#39;cell_constraints&#39;</span><span class="p">,</span> <span class="s1">&#39;external_pressure&#39;</span><span class="p">,</span> <span class="s1">&#39;fix_all_ions&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;fix_all_cell&#39;</span><span class="p">,</span> <span class="s1">&#39;species_mass&#39;</span><span class="p">,</span> <span class="s1">&#39;species_pot&#39;</span><span class="p">,</span> <span class="s1">&#39;ionic_velocities&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;species_lcao_states&#39;</span><span class="p">,</span> <span class="s1">&#39;kpoints_list&#39;</span><span class="p">,</span> <span class="s1">&#39;kpoints_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;kpoints_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;kpoints_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;kpoint_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;kpoint_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoint_path&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoint_path_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;bs_kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoint_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoint_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;bs_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoints_path&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoints_path_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;bs_kpoints_list&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoints_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_kpoints_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;bs_kpoints_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_supercell_matrix&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_kpoint_path&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;phonon_kpoint_path_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_kpoint_mp_grid&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;phonon_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_kpoint_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;phonon_gamma_directions&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_kpoints_path&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_kpoints_path_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;phonon_kpoints_list&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_fine_kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_fine_kpoint_path&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;phonon_fine_kpoint_path_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_fine_kpoint_mp_grid&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;phonon_fine_kpoint_mp_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_fine_kpoint_mp_offset&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;optics_kpoints_list&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_kpoints_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_kpoints_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;optics_kpoints_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_kpoint_mp_grid&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;optics_kpoint_mp_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_kpoint_list&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;magres_kpoint_path&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_kpoint_path_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_kpoint_mp_grid&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;magres_kpoint_mp_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;positions_frac_product&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;positions_abs_product&#39;</span><span class="p">,</span> <span class="s1">&#39;positions_frac_intermediate&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;positions_abs_intermediate&#39;</span><span class="p">,</span> <span class="s1">&#39;fix_vol&#39;</span><span class="p">,</span> <span class="s1">&#39;species_gamma&#39;</span><span class="p">,</span> <span class="s1">&#39;species_q&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;supercell_kpoints_list&#39;</span><span class="p">,</span> <span class="s1">&#39;supercell_kpoints_mp_grid&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;supercell_kpoints_mp_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;supercell_kpoints_mp_offset&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;supercell_kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;supercell_kpoint_mp_grid&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;supercell_kpoint_mp_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;supercell_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;supercell_matrix&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;nonlinear_constraints&#39;</span><span class="p">,</span> <span class="s1">&#39;external_efield&#39;</span><span class="p">,</span> <span class="s1">&#39;positions_noise&#39;</span><span class="p">,</span> <span class="s1">&#39;cell_noise&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;hubbard_u&#39;</span><span class="p">,</span> <span class="s1">&#39;hubbard_alpha&#39;</span><span class="p">,</span> <span class="s1">&#39;atomic_init&#39;</span><span class="p">,</span> <span class="s1">&#39;quantisation_axis&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;quantization_axis&#39;</span><span class="p">,</span> <span class="s1">&#39;jcoupling_site&#39;</span><span class="p">,</span> <span class="s1">&#39;chemical_potential&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;elnes_kpoint_list&#39;</span><span class="p">,</span> <span class="s1">&#39;elnes_kpoint_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;elnes_kpoint_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;elnes_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;snap_to_symmetry&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_kpoint_path&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;spectral_kpoint_path_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_kpoint_list&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;spectral_kpoint_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_kpoint_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;spectral_kpoint_mp_offset&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_kpoints_path&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;spectral_kpoints_path_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_kpoints_list&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;spectral_kpoints_mp_grid&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_kpoints_mp_spacing&#39;</span><span class="p">,</span>
                       <span class="s1">&#39;spectral_kpoints_mp_offset&#39;</span><span class="p">]</span>

<span class="n">valid_parameters_keywords</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;comment&#39;</span><span class="p">,</span> <span class="s1">&#39;iprint&#39;</span><span class="p">,</span> <span class="s1">&#39;continuation&#39;</span><span class="p">,</span> <span class="s1">&#39;reuse&#39;</span><span class="p">,</span> <span class="s1">&#39;checkpoint&#39;</span><span class="p">,</span> <span class="s1">&#39;task&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;calculate_stress&#39;</span><span class="p">,</span> <span class="s1">&#39;calculate_densdiff&#39;</span><span class="p">,</span> <span class="s1">&#39;calculate_elf&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;calculate_hirshfeld&#39;</span><span class="p">,</span> <span class="s1">&#39;run_time&#39;</span><span class="p">,</span> <span class="s1">&#39;backup_interval&#39;</span><span class="p">,</span> <span class="s1">&#39;num_backup_iter&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;print_clock&#39;</span><span class="p">,</span> <span class="s1">&#39;print_memory_usage&#39;</span><span class="p">,</span> <span class="s1">&#39;write_formatted_potential&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;write_formatted_density&#39;</span><span class="p">,</span> <span class="s1">&#39;write_formatted_elf&#39;</span><span class="p">,</span> <span class="s1">&#39;write_orbitals&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;write_cif_structure&#39;</span><span class="p">,</span> <span class="s1">&#39;write_cell_structure&#39;</span><span class="p">,</span> <span class="s1">&#39;write_bib&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;calc_molecular_dipole&#39;</span><span class="p">,</span> <span class="s1">&#39;write_checkpoint&#39;</span><span class="p">,</span> <span class="s1">&#39;cml_output&#39;</span><span class="p">,</span> <span class="s1">&#39;cml_filename&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;length_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;mass_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;time_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;charge_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;spin_unit&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;energy_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;force_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;velocity_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;pressure_unit&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;inv_length_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;frequency_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;force_constant_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;volume_unit&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;ir_intensity_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;dipole_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;entropy_unit&#39;</span><span class="p">,</span> <span class="s1">&#39;page_wvfns&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;rand_seed&#39;</span><span class="p">,</span> <span class="s1">&#39;data_distribution&#39;</span><span class="p">,</span> <span class="s1">&#39;opt_strategy&#39;</span><span class="p">,</span> <span class="s1">&#39;opt_strategy_bias&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;num_farms&#39;</span><span class="p">,</span> <span class="s1">&#39;num_proc_in_smp&#39;</span><span class="p">,</span> <span class="s1">&#39;num_proc_in_smp_fine&#39;</span><span class="p">,</span> <span class="s1">&#39;message_size&#39;</span><span class="p">,</span> <span class="s1">&#39;stop&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;xc_functional&#39;</span><span class="p">,</span> <span class="s1">&#39;xc_vxc_deriv_epsilon&#39;</span><span class="p">,</span> <span class="s1">&#39;relativistic_treatment&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_apply&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;sedc_scheme&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_sr_ts&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_d_ts&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_s6_g06&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_d_g06&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;sedc_lambda_obs&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_n_obs&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_sr_jchs&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_s6_jchs&#39;</span><span class="p">,</span> <span class="s1">&#39;sedc_d_jchs&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;page_ex_pot&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_page_ex_pot&#39;</span><span class="p">,</span> <span class="s1">&#39;ppd_integral&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_ppd_integral&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;ppd_size_x&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_ppd_size_x&#39;</span><span class="p">,</span> <span class="s1">&#39;ppd_size_y&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_ppd_size_y&#39;</span><span class="p">,</span> <span class="s1">&#39;ppd_size_z&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;nlxc_ppd_size_z&#39;</span><span class="p">,</span> <span class="s1">&#39;impose_trs&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_impose_trs&#39;</span><span class="p">,</span> <span class="s1">&#39;exchange_reflect_kpts&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;nlxc_exchange_reflect_kpts&#39;</span><span class="p">,</span> <span class="s1">&#39;k_scrn_den_function&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_k_scrn_den_function&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;k_scrn_averaging_scheme&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_k_scrn_averaging_scheme&#39;</span><span class="p">,</span> <span class="s1">&#39;re_est_k_scrn&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;nlxc_re_est_k_scrn&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_exchange_screening&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_exchange_fraction&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;calc_full_ex_pot&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_calc_full_ex_pot&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_div_corr_on&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;nlxc_div_corr_s_width&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_div_corr_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;nlxc_div_corr_npts_step&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;pspot_nonlocal_type&#39;</span><span class="p">,</span> <span class="s1">&#39;pspot_beta_phi_type&#39;</span><span class="p">,</span> <span class="s1">&#39;basis_precision&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;cut_off_energy&#39;</span><span class="p">,</span> <span class="s1">&#39;grid_scale&#39;</span><span class="p">,</span> <span class="s1">&#39;fine_grid_scale&#39;</span><span class="p">,</span> <span class="s1">&#39;fine_gmax&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;fft_max_prime_factor&#39;</span><span class="p">,</span> <span class="s1">&#39;fixed_npw&#39;</span><span class="p">,</span> <span class="s1">&#39;finite_basis_corr&#39;</span><span class="p">,</span> <span class="s1">&#39;basis_de_dloge&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;finite_basis_npoints&#39;</span><span class="p">,</span> <span class="s1">&#39;finite_basis_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;nelectrons&#39;</span><span class="p">,</span> <span class="s1">&#39;charge&#39;</span><span class="p">,</span> <span class="s1">&#39;spin&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;nup&#39;</span><span class="p">,</span> <span class="s1">&#39;ndown&#39;</span><span class="p">,</span> <span class="s1">&#39;spin_polarized&#39;</span><span class="p">,</span> <span class="s1">&#39;spin_polarised&#39;</span><span class="p">,</span> <span class="s1">&#39;nspins&#39;</span><span class="p">,</span> <span class="s1">&#39;nextra_bands&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;perc_extra_bands&#39;</span><span class="p">,</span> <span class="s1">&#39;nbands&#39;</span><span class="p">,</span> <span class="s1">&#39;elec_temp&#39;</span><span class="p">,</span> <span class="s1">&#39;electronic_minimizer&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;max_sd_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;max_cg_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;max_diis_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;elec_method&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;metals_method&#39;</span><span class="p">,</span> <span class="s1">&#39;elec_energy_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;elec_eigenvalue_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;elec_force_tol&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;elec_convergence_win&#39;</span><span class="p">,</span> <span class="s1">&#39;max_scf_cycles&#39;</span><span class="p">,</span> <span class="s1">&#39;spin_fix&#39;</span><span class="p">,</span> <span class="s1">&#39;fix_occupancy&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;smearing_scheme&#39;</span><span class="p">,</span> <span class="s1">&#39;smearing_width&#39;</span><span class="p">,</span> <span class="s1">&#39;efermi_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;num_occ_cycles&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;dipole_correction&#39;</span><span class="p">,</span> <span class="s1">&#39;dipole_dir&#39;</span><span class="p">,</span> <span class="s1">&#39;elec_dump_file&#39;</span><span class="p">,</span> <span class="s1">&#39;num_dump_cycles&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;elec_restore_file&#39;</span><span class="p">,</span> <span class="s1">&#39;mixing_scheme&#39;</span><span class="p">,</span> <span class="s1">&#39;mix_history_length&#39;</span><span class="p">,</span> <span class="s1">&#39;mix_charge_amp&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;mix_charge_gmax&#39;</span><span class="p">,</span> <span class="s1">&#39;mix_spin_amp&#39;</span><span class="p">,</span> <span class="s1">&#39;mix_spin_gmax&#39;</span><span class="p">,</span> <span class="s1">&#39;mix_cut_off_energy&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;mix_metric_q&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_calculate&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_bond_cutoff&#39;</span><span class="p">,</span> <span class="s1">&#39;pdos_calculate_weights&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;bs_max_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_max_cg_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_nextra_bands&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_perc_extra_bands&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;bs_nbands&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_eigenvalue_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_xc_functional&#39;</span><span class="p">,</span> <span class="s1">&#39;bs_re_est_k_scrn&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;bs_write_eigenvalues&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_method&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_max_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_energy_tol&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;geom_force_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_disp_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_stress_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_convergence_win&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;geom_modulus_est&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_frequency_est&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_spin_fix&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_use_linmin&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;geom_linmin_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_lbfgs_max_updates&#39;</span><span class="p">,</span> <span class="s1">&#39;geom_tpsd_init_stepsize&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;geom_tpsd_iterchange&#39;</span><span class="p">,</span> <span class="s1">&#39;md_num_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;md_delta_t&#39;</span><span class="p">,</span> <span class="s1">&#39;md_ensemble&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_use_pathint&#39;</span><span class="p">,</span> <span class="s1">&#39;md_num_beads&#39;</span><span class="p">,</span> <span class="s1">&#39;md_pathint_init&#39;</span><span class="p">,</span> <span class="s1">&#39;md_pathint_staging&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_pathint_num_stages&#39;</span><span class="p">,</span> <span class="s1">&#39;md_temperature&#39;</span><span class="p">,</span> <span class="s1">&#39;md_thermostat&#39;</span><span class="p">,</span> <span class="s1">&#39;md_barostat&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_ion_t&#39;</span><span class="p">,</span> <span class="s1">&#39;md_cell_t&#39;</span><span class="p">,</span> <span class="s1">&#39;md_nhc_length&#39;</span><span class="p">,</span> <span class="s1">&#39;md_nose_t&#39;</span><span class="p">,</span> <span class="s1">&#39;md_langevin_t&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_extrap&#39;</span><span class="p">,</span> <span class="s1">&#39;md_extrap_fit&#39;</span><span class="p">,</span> <span class="s1">&#39;md_xlbomd&#39;</span><span class="p">,</span> <span class="s1">&#39;md_xlbomd_history&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_damping_scheme&#39;</span><span class="p">,</span> <span class="s1">&#39;md_damping_reset&#39;</span><span class="p">,</span> <span class="s1">&#39;md_opt_damped_delta_t&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_elec_energy_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;md_elec_eigenvalue_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;md_elec_force_tol&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_elec_convergence_win&#39;</span><span class="p">,</span> <span class="s1">&#39;md_sample_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;md_eqm_method&#39;</span><span class="p">,</span> <span class="s1">&#39;md_eqm_ion_t&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;md_eqm_cell_t&#39;</span><span class="p">,</span> <span class="s1">&#39;md_eqm_t&#39;</span><span class="p">,</span> <span class="s1">&#39;md_use_plumed&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_nextra_bands&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;optics_perc_extra_bands&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_nbands&#39;</span><span class="p">,</span> <span class="s1">&#39;optics_xc_functional&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;tssearch_method&#39;</span><span class="p">,</span> <span class="s1">&#39;tssearch_lstqst_protocol&#39;</span><span class="p">,</span> <span class="s1">&#39;tssearch_qst_max_iter&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;tssearch_cg_max_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;tssearch_max_path_points&#39;</span><span class="p">,</span> <span class="s1">&#39;tssearch_force_tol&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;tssearch_disp_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;tssearch_energy_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_const_basis&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_energy_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_max_cg_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_max_cycles&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_convergence_win&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_preconditioner&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_use_kpoint_symmetry&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_calculate_dos&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_dos_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_dos_limit&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_finite_disp&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_fine_cutoff_method&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_force_constant_cutoff&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_force_constant_cut_scale&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_force_constant_ellipsoid&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_fine_method&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_method&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_dfpt_method&#39;</span><span class="p">,</span> <span class="s1">&#39;secondd_method&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_calc_lo_to_splitting&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;phonon_sum_rule&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_sum_rule_method&#39;</span><span class="p">,</span> <span class="s1">&#39;calculate_born_charges&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;born_charge_sum_rule&#39;</span><span class="p">,</span> <span class="s1">&#39;calculate_raman&#39;</span><span class="p">,</span> <span class="s1">&#39;raman_range_low&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;raman_range_high&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_write_force_constants&#39;</span><span class="p">,</span> <span class="s1">&#39;phonon_write_dynamical&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;efield_dfpt_method&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_max_cg_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_max_cycles&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;efield_convergence_win&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_energy_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_calc_ion_permittivity&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;efield_ignore_molec_modes&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_freq_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;efield_oscillator_q&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;thermo_calculate_helmholtz&#39;</span><span class="p">,</span> <span class="s1">&#39;thermo_t_start&#39;</span><span class="p">,</span> <span class="s1">&#39;thermo_t_stop&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;thermo_t_spacing&#39;</span><span class="p">,</span> <span class="s1">&#39;thermo_t_npoints&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_spread_tol&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;wannier_max_sd_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_sd_step&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_print_cube&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;wannier_spread_type&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_min_algor&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_ion_moments&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;wannier_ion_rmax&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_ion_cut&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_ion_cut_fraction&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;wannier_restart&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_ion_cut_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;wannier_ion_cmoments&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_task&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;magres_method&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_max_cg_steps&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_convergence_win&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;magres_conv_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_xc_functional&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_max_sc_cycles&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;magres_jcoupling_task&#39;</span><span class="p">,</span> <span class="s1">&#39;magres_write_response&#39;</span><span class="p">,</span> <span class="s1">&#39;elnes_nextra_bands&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;elnes_perc_extra_bands&#39;</span><span class="p">,</span> <span class="s1">&#39;elnes_nbands&#39;</span><span class="p">,</span> <span class="s1">&#39;elnes_xc_functional&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;elnes_eigenvalue_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_theory&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_task&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_max_iter&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;spectral_max_steps_per_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_nextra_bands&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;spectral_perc_extra_bands&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_nbands&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_eigenvalue_tol&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;spectral_xc_functional&#39;</span><span class="p">,</span> <span class="s1">&#39;spectral_re_est_k_scrn&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;spectral_write_eigenvalues&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_num_states&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_selected_state&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;tddft_eigenvalue_tol&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_convergence_win&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_max_iter&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;tddft_nextra_states&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_xc_functional&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_method&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;tddft_eigenvalue_method&#39;</span><span class="p">,</span> <span class="s1">&#39;tddft_approximation&#39;</span><span class="p">,</span> <span class="s1">&#39;ga_pop_size&#39;</span><span class="p">,</span> <span class="s1">&#39;ga_max_gens&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;ga_mutate_rate&#39;</span><span class="p">,</span> <span class="s1">&#39;ga_mutate_amp&#39;</span><span class="p">,</span> <span class="s1">&#39;ga_fixed_n&#39;</span><span class="p">,</span> <span class="s1">&#39;ga_bulk_slice&#39;</span><span class="p">,</span>
                             <span class="s1">&#39;excited_state_scissors&#39;</span><span class="p">,</span> <span class="s1">&#39;devel_code&#39;</span><span class="p">]</span>

<div class="viewcode-block" id="CastepCell"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepCell">[docs]</a><span class="k">class</span> <span class="nc">CastepCell</span><span class="p">(</span><span class="n">OrderedDict</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Class to wrap a CASTEP cell (.cell) file&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cellfile</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">xml</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">atoms</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">OrderedDict</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">cellfile</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">cellfile</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">xml</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read_xml</span><span class="p">(</span><span class="n">xml</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">atoms</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update_from_atoms</span><span class="p">(</span><span class="n">atoms</span><span class="p">)</span>

<div class="viewcode-block" id="CastepCell.copy"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepCell.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">new</span> <span class="o">=</span> <span class="n">CastepCell</span><span class="p">()</span>
        <span class="n">new</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">new</span></div>

<div class="viewcode-block" id="CastepCell.read"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepCell.read">[docs]</a>    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cellfile</span><span class="p">):</span>
        <span class="s2">&quot;Read a CASTEP .cell file. cellfile can be a mapping type, filename or an open file&quot;</span>

        <span class="k">if</span> <span class="n">operator</span><span class="o">.</span><span class="n">isMappingType</span><span class="p">(</span><span class="n">cellfile</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">cellfile</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">cellfile</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
            <span class="n">cellfile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">cellfile</span><span class="p">,</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>

        <span class="n">current_block</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">cellfile</span><span class="p">:</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

            <span class="c1"># Skip comments and blank lines</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;!&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">line</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="c1"># Start of block</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;%BLOCK&#39;</span><span class="p">):</span>
                <span class="n">block</span><span class="p">,</span> <span class="n">name</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">strip</span><span class="p">,</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
                <span class="k">if</span> <span class="n">current_block</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">current_block</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
                    <span class="bp">self</span><span class="p">[</span><span class="n">current_block</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Parse error in cell file: can&#39;t begin new block </span><span class="si">%s</span><span class="s2"> when already in block </span><span class="si">%s</span><span class="s2">&quot;</span> \
                        <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">current_block</span><span class="p">))</span>

            <span class="c1"># End of block</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%E</span><span class="s1">NDBLOCK&#39;</span><span class="p">):</span>
                <span class="n">block</span><span class="p">,</span> <span class="n">name</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">strip</span><span class="p">,</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
                <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="n">current_block</span><span class="p">:</span>
                    <span class="n">current_block</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Parse error in cell file: endblock </span><span class="si">%s</span><span class="s1"> does not match </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">current_block</span><span class="p">))</span>

            <span class="c1"># Stand-alone line</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">current_block</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="bp">self</span><span class="p">[</span><span class="n">current_block</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="s1">&#39;:=&#39;</span><span class="p">:</span>  <span class="c1"># Remove delimeters</span>
                            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>

                        <span class="n">fields</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                        <span class="n">key</span> <span class="o">=</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">valid_cell_keywords</span><span class="p">:</span>
                            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown CELL keyword </span><span class="si">%s</span><span class="s1">&#39;</span><span class="o">%</span> <span class="n">key</span><span class="p">)</span>
                        <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Error parsing cell file line: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span></div>


    <span class="k">def</span> <span class="nf">read_xml</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xml</span><span class="p">):</span>
        <span class="n">els</span> <span class="o">=</span> <span class="n">xml</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;castep_cell&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">els</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No &lt;castep_cell&gt; element in XML file&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;More than one &lt;castep_cell&gt; element in XML file&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">childNodes</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">firstChild</span><span class="o">.</span><span class="n">nodeType</span> <span class="o">!=</span> <span class="n">xml</span><span class="o">.</span><span class="n">TEXT_NODE</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;&lt;castep_cell&gt; element should have exactly one Text node&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">firstChild</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">))</span>


<div class="viewcode-block" id="CastepCell.write"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepCell.write">[docs]</a>    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cellfile</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">):</span>
        <span class="s2">&quot;Write CASTEP .cell file. cellfile can be a filename or an open file&quot;</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">cellfile</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
            <span class="n">cellfile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">cellfile</span><span class="p">,</span><span class="s1">&#39;w&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">([]):</span>
                <span class="n">cellfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">%BLOCK &#39;</span><span class="o">+</span><span class="n">key</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
                    <span class="n">cellfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;  &#39;</span><span class="o">+</span><span class="n">line</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="n">cellfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%E</span><span class="s1">NDBLOCK &#39;</span><span class="o">+</span><span class="n">key</span><span class="o">+</span><span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;[&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;]&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
                <span class="n">cellfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">  </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span></div>


    <span class="k">def</span> <span class="nf">to_atoms</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;LATTICE_CART&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;LATTICE_ABC&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;cell is missing LATTICE_CART and LATTICE_ABC blocks&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;LATTICE_CART&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;LATTICE_ABC&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;cell has both LATTICE_CART and LATTICE_ABC&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;POSITIONS_ABS&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;POSITIONS_FRAC&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;cell is missing POSITIONS_ABS and POSITIONS_FRAC blocks&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;POSITIONS_ABS&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;POSITIONS_FRAC&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;cell has both POSITIONS_ABS and POSITIONS_FRAC&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;POSITIONS_ABS&#39;</span><span class="p">):</span> <span class="n">pos_block</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;POSITIONS_ABS&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>                             <span class="n">pos_block</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;POSITIONS_FRAC&#39;</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;LATTICE_CART&#39;</span><span class="p">):</span>
            <span class="n">block</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_CART&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">block</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
                <span class="n">unit</span> <span class="o">=</span> <span class="n">block</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">unit</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">castep_units</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Don&#39;t know how to convert from units of </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">block</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                <span class="n">block</span> <span class="o">=</span> <span class="n">block</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">unit</span> <span class="o">=</span> <span class="s1">&#39;ang&#39;</span>

            <span class="n">lattice</span> <span class="o">=</span> <span class="n">farray</span><span class="p">([</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">row</span><span class="p">]</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">split</span><span class="p">,</span> <span class="n">block</span><span class="p">)</span> <span class="p">])</span><span class="o">.</span><span class="n">T</span><span class="o">*</span><span class="n">castep_units</span><span class="p">[</span><span class="n">unit</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">block</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_ABC&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">block</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                <span class="n">unit</span> <span class="o">=</span> <span class="n">block</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">unit</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">castep_units</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Don&#39;t know how to convert from units of </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">block</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                <span class="n">block</span> <span class="o">=</span> <span class="n">block</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">unit</span> <span class="o">=</span> <span class="s1">&#39;ang&#39;</span>

            <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">castep_units</span><span class="p">[</span><span class="n">unit</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">block</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>
            <span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span> <span class="o">=</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="o">/</span><span class="mf">180.0</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">block</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>

            <span class="n">lattice</span> <span class="o">=</span> <span class="n">make_lattice</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">,</span><span class="n">alpha</span><span class="p">,</span><span class="n">beta</span><span class="p">,</span><span class="n">gamma</span><span class="p">)</span>

        <span class="c1"># Check if first line in position block is a valid unit</span>
        <span class="n">unit</span> <span class="o">=</span> <span class="n">pos_block</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">unit</span> <span class="ow">in</span> <span class="n">castep_units</span><span class="p">:</span>
            <span class="n">pos_block</span> <span class="o">=</span> <span class="n">pos_block</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">unit</span> <span class="o">=</span> <span class="s1">&#39;ang&#39;</span>

        <span class="n">atoms</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">pos_block</span><span class="p">),</span><span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">)</span>

        <span class="n">field_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">pos_block</span><span class="p">]</span>
        <span class="n">elements</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">field_list</span><span class="p">)</span>

        <span class="c1"># Look up names of elements specified by atomic number</span>
        <span class="n">elements</span> <span class="o">=</span> <span class="p">[</span> <span class="ow">not</span> <span class="n">el</span><span class="o">.</span><span class="n">isdigit</span><span class="p">()</span> <span class="ow">and</span> <span class="n">atomic_number</span><span class="p">(</span><span class="n">el</span><span class="p">)</span> <span class="ow">or</span> <span class="n">el</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span> <span class="p">]</span>

        <span class="c1"># Set the element and pos data</span>
        <span class="n">atoms</span><span class="o">.</span><span class="n">set_atoms</span><span class="p">(</span><span class="n">elements</span><span class="p">)</span>
        <span class="n">atoms</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,:]</span> <span class="o">=</span> <span class="n">farray</span><span class="p">([</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">castep_units</span><span class="p">[</span><span class="n">unit</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">row</span><span class="p">]</span> \
                                  <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="p">[</span><span class="n">field</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">field_list</span><span class="p">]])</span><span class="o">.</span><span class="n">T</span>

        <span class="c1"># Convert from fractional to real positions</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;POSITIONS_FRAC&#39;</span><span class="p">):</span>
            <span class="n">atoms</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">atoms</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span> <span class="n">atoms</span><span class="o">.</span><span class="n">pos</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">atoms</span>

    <span class="k">def</span> <span class="nf">update_from_atoms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">,</span> <span class="n">frac_pos</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">lattice_abc</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>

        <span class="c1"># Add lattice to cell</span>
        <span class="k">if</span> <span class="n">lattice_abc</span><span class="p">:</span>
            <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_ABC&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">gamma</span> <span class="o">=</span> <span class="n">get_lattice_params</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">)</span>
            <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_ABC&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">))</span>
            <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_ABC&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">alpha</span><span class="o">*</span><span class="mf">180.0</span><span class="o">/</span><span class="n">pi</span><span class="p">,</span><span class="n">beta</span><span class="o">*</span><span class="mf">180.0</span><span class="o">/</span><span class="n">pi</span><span class="p">,</span><span class="n">gamma</span><span class="o">*</span><span class="mf">180.0</span><span class="o">/</span><span class="n">pi</span><span class="p">))</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_CART&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
                <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;LATTICE_CART&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="n">i</span><span class="p">]))</span>

        <span class="c1"># Add atomic positions to cell</span>
        <span class="k">if</span> <span class="n">frac_pos</span><span class="p">:</span>
            <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;POSITIONS_FRAC&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;POSITIONS_FRAC&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:,</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">+</span><span class="s1">&#39; </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">g</span><span class="p">,</span><span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">i</span><span class="p">])))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;POSITIONS_ABS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="bp">self</span><span class="p">[</span><span class="s1">&#39;POSITIONS_ABS&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:,</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">+</span><span class="s1">&#39; </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1"> </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">i</span><span class="p">]))</span>

        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">valid_cell_keywords</span><span class="p">:</span>
                <span class="bp">self</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">p</span><span class="p">]</span>


    <span class="nd">@staticmethod</span>
    <span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;cell&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">cellreader</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="n">CastepCell</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
        <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_atoms</span><span class="p">()</span></div>

<span class="k">class</span> <span class="nc">CastepCellWriter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dest</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">dest</span> <span class="o">==</span> <span class="s1">&#39;stdout&#39;</span><span class="p">:</span> <span class="n">dest</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dest</span> <span class="o">=</span> <span class="n">dest</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">,</span> <span class="n">cell_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">param_template</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dest</span>
        <span class="k">if</span> <span class="s1">&#39;%&#39;</span> <span class="ow">in</span> <span class="n">dest</span><span class="p">:</span> <span class="n">dest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dest</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span>
        <span class="k">if</span> <span class="n">cell_template</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">cell</span> <span class="o">=</span> <span class="n">CastepCell</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cell</span> <span class="o">=</span> <span class="n">CastepCell</span><span class="p">(</span><span class="n">cell_template</span><span class="p">)</span>
        <span class="n">cell</span><span class="o">.</span><span class="n">update_from_atoms</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>
        <span class="n">cell</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">param_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">param</span> <span class="o">=</span> <span class="n">CastepParam</span><span class="p">(</span><span class="n">param_template</span><span class="p">)</span>
            <span class="n">param</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">dest</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;.param&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>

<span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;cell&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">CastepCellWriter</span>

<div class="viewcode-block" id="CastepParam"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepParam">[docs]</a><span class="k">class</span> <span class="nc">CastepParam</span><span class="p">(</span><span class="n">OrderedDict</span><span class="p">):</span>
    <span class="s2">&quot;Class to wrap a CASTEP parameter (.param) file&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">paramfile</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">xml</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">atoms</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">OrderedDict</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">paramfile</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">paramfile</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">xml</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read_xml</span><span class="p">(</span><span class="n">xml</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">atoms</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update_from_atoms</span><span class="p">(</span><span class="n">atoms</span><span class="p">)</span>

<div class="viewcode-block" id="CastepParam.copy"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepParam.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">new</span> <span class="o">=</span> <span class="n">CastepParam</span><span class="p">()</span>
        <span class="n">new</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">new</span></div>

<div class="viewcode-block" id="CastepParam.read"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepParam.read">[docs]</a>    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">paramfile</span><span class="p">):</span>
        <span class="s2">&quot;Read a CASTEP .param file. paramfile can be a filename or an open file&quot;</span>

        <span class="k">if</span> <span class="n">operator</span><span class="o">.</span><span class="n">isMappingType</span><span class="p">(</span><span class="n">paramfile</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">paramfile</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">paramfile</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
            <span class="n">paramfile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">paramfile</span><span class="p">,</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">paramfile</span><span class="p">:</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

            <span class="c1"># Skip comments and blank lines</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;!&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">line</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">line</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">&#39;[:=]&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">valid_parameters_keywords</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown PARAMETERS keyword </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
            <span class="n">value</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">castep_value_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
            <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span></div>

    <span class="k">def</span> <span class="nf">read_xml</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xml</span><span class="p">):</span>
        <span class="n">els</span> <span class="o">=</span> <span class="n">xml</span><span class="o">.</span><span class="n">documentElement</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;castep_param&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">els</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No &lt;castep_param&gt; element in XML file&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;More than one &lt;castep_param&gt; element in XML file&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">childNodes</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">firstChild</span><span class="o">.</span><span class="n">nodeType</span> <span class="o">!=</span> <span class="n">xml</span><span class="o">.</span><span class="n">TEXT_NODE</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;&lt;castep_param&gt; element should have exactly one Text node&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">els</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">firstChild</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">))</span>

<div class="viewcode-block" id="CastepParam.read_from_castep_output"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepParam.read_from_castep_output">[docs]</a>    <span class="k">def</span> <span class="nf">read_from_castep_output</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">castep_output</span><span class="p">):</span>
        <span class="s2">&quot;Read user parameters from .castep output. Input should be filename, file-like object or list of lines&quot;</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
            <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">castep_output</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
            <span class="n">castep_output</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">castep_output</span><span class="p">,</span> <span class="s1">&#39;read&#39;</span><span class="p">):</span>
            <span class="n">castep_output</span> <span class="o">=</span> <span class="n">castep_output</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

        <span class="c1"># Remove newlines from end of each line in castep_output</span>
        <span class="n">castep_output</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">castep_output</span><span class="p">]</span>

        <span class="c1"># Bizarrelly, the parameter names in the output file have the underscores</span>
        <span class="c1"># removed from them. Here we construct a mapping from the .castep names to</span>
        <span class="c1"># the true .param file</span>
        <span class="n">param_lookup</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">),</span><span class="n">valid_parameters_keywords</span><span class="p">),</span>
             <span class="n">valid_parameters_keywords</span><span class="p">))</span>

        <span class="c1"># Find the user parameters section</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">user_param_start</span> <span class="o">=</span> <span class="n">castep_output</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39; ******************************* User Parameters *******************************&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No user parameters found in castep output&#39;</span><span class="p">)</span>

        <span class="n">param_lines</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">user_param_start</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">while</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">():</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">strip</span><span class="p">,</span> <span class="n">line</span><span class="p">[:</span><span class="n">line</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">param_lookup</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown parameter </span><span class="si">%s</span><span class="s1"> in castep output file&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
            <span class="n">param_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> = </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">param_lookup</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">value</span><span class="p">))</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">param_lines</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">update_from_atoms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">valid_parameters_keywords</span><span class="p">:</span>
                <span class="bp">self</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">p</span><span class="p">]</span>

<div class="viewcode-block" id="CastepParam.write"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepParam.write">[docs]</a>    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">paramfile</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">):</span>
        <span class="s2">&quot;Write CASTEP .param file&quot;</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">paramfile</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
            <span class="n">paramfile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">paramfile</span><span class="p">,</span><span class="s1">&#39;w&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">castep_output_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
            <span class="n">paramfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> = </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span></div></div>

<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;geom&#39;</span><span class="p">)</span>
<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;md&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">CastepGeomMDReader</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">atoms_ref</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generator to read frames from CASTEP .geom and .md files&quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">source</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="n">source</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">source</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">has_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">):</span>
        <span class="n">atoms_ref</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
        <span class="n">atoms_ref</span><span class="o">.</span><span class="n">frac_pos</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">atoms_ref</span><span class="o">.</span><span class="n">g</span><span class="p">,</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">pos</span><span class="p">)</span>

    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">line</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">next</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="c1"># raises StopIteration at end of file</span>

        <span class="c1"># Skip header if present</span>
        <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;BEGIN header&#39;</span><span class="p">):</span>
            <span class="k">while</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;END header&#39;</span><span class="p">):</span>
                <span class="n">line</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">next</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="n">source</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="c1"># skip blank line</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>

        <span class="c1"># Read until next blank line</span>
        <span class="k">while</span> <span class="n">line</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">source</span><span class="o">.</span><span class="n">next</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">line</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span> <span class="n">lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">StopIteration</span>

        <span class="n">params</span> <span class="o">=</span> <span class="n">Dictionary</span><span class="p">()</span>

        <span class="c1"># First line is the time/step in a.u - we convert to fs</span>
        <span class="n">params</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">*</span><span class="n">AU_FS</span>

        <span class="c1"># Then the energy, in Hartree - we convert to eV</span>
        <span class="n">energy_lines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- E&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">energy_lines</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Number of energy lines should be exactly one. Got </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">energy_lines</span><span class="p">)</span>

        <span class="n">params</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">],</span> <span class="n">params</span><span class="p">[</span><span class="s1">&#39;hamiltonian&#39;</span><span class="p">]</span> <span class="o">=</span> \
                               <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">HARTREE</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">energy_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]]</span>

        <span class="c1"># Temperature, in atomic units - we convert to Kelvin</span>
        <span class="n">temperature_lines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- T&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">temperature_lines</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">temperature_lines</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Number of temperature lines should be exactly one. Got </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">temperature_lines</span><span class="p">)</span>
            <span class="n">params</span><span class="p">[</span><span class="s1">&#39;temperature&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">temperature_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span><span class="o">*</span><span class="n">HARTREE</span><span class="o">/</span><span class="n">BOLTZMANN_K</span>

        <span class="c1"># Pressure, in atomic units - we convert to ev/A**3</span>
        <span class="n">pressure_lines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- P&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">pressure_lines</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pressure_lines</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Number of pressure lines should be exactly one. Got </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">pressure_lines</span><span class="p">)</span>
            <span class="n">params</span><span class="p">[</span><span class="s1">&#39;pressure&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">pressure_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span><span class="o">*</span><span class="n">HARTREE</span><span class="o">/</span><span class="n">BOHR</span><span class="o">**</span><span class="mi">3</span>

        <span class="c1"># Lattice is next, in units of Bohr</span>
        <span class="n">lattice_lines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- h&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">lattice_lines</span><span class="p">:</span>
            <span class="n">lattice</span> <span class="o">=</span> <span class="n">farray</span><span class="p">([</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span> <span class="n">BOHR</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]]</span>
                               <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">split</span><span class="p">,</span> <span class="n">lattice_lines</span><span class="p">)</span> <span class="p">])</span><span class="o">.</span><span class="n">T</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No lattice in .geom file and atoms_ref not present&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">lattice</span> <span class="o">=</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:]</span>

        <span class="c1"># Then optionally virial tensor - convert stress tensor to eV/A**3</span>
        <span class="c1"># then multiply by cell volume to get virial in eV</span>
        <span class="n">stress_lines</span>  <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- S&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">stress_lines</span><span class="p">:</span>
            <span class="n">virial</span> <span class="o">=</span> <span class="n">farray</span><span class="p">([</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">HARTREE</span><span class="o">/</span><span class="p">(</span><span class="n">BOHR</span><span class="o">**</span><span class="mi">3</span><span class="p">))</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]]</span>
                              <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">split</span><span class="p">,</span> <span class="n">stress_lines</span><span class="p">)</span> <span class="p">])</span><span class="o">.</span><span class="n">T</span>

        <span class="c1"># Find positions and forces</span>
        <span class="n">poslines</span>   <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- R&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="n">velolines</span>  <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- V&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>
        <span class="n">forcelines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;-- F&#39;</span><span class="p">),</span> <span class="n">lines</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">poslines</span> <span class="ow">and</span> <span class="n">forcelines</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">poslines</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">forcelines</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Number of pos lines (</span><span class="si">%d</span><span class="s1">) != force lines (</span><span class="si">%d</span><span class="s1">)&#39;</span>
                             <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">poslines</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">forcelines</span><span class="p">)))</span>

        <span class="n">n_atoms</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">poslines</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">forcelines</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">poslines</span> <span class="o">==</span> <span class="p">[]</span> <span class="ow">and</span> <span class="n">forcelines</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No positions or forces in .geom file and atoms_ref not present&#39;</span><span class="p">)</span>
            <span class="n">n_atoms</span> <span class="o">=</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">n</span>

        <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># If we were passed in an Atoms object, construct mapping from</span>
            <span class="c1"># CASTEP (element, number) to original atom index</span>
            <span class="k">assert</span> <span class="n">n_atoms</span> <span class="o">==</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">n</span>
            <span class="n">at</span> <span class="o">=</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

            <span class="c1"># remove things which shouldn&#39;t be inherited from atoms_ref</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="s1">&#39;hamiltonian&#39;</span><span class="p">,</span> <span class="s1">&#39;temperature&#39;</span><span class="p">,</span> <span class="s1">&#39;pressure&#39;</span><span class="p">,</span> <span class="s1">&#39;virial&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">:</span> <span class="k">del</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">p</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="s1">&#39;velo&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">properties</span><span class="p">:</span> <span class="k">del</span> <span class="n">at</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="n">p</span><span class="p">]</span>

            <span class="n">at</span><span class="o">.</span><span class="n">set_lattice</span><span class="p">(</span><span class="n">lattice</span><span class="p">,</span> <span class="n">scale_positions</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
            <span class="n">species_count</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="n">el</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">species_count</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">el</span><span class="p">):</span>
                    <span class="n">species_count</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">species_count</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">species_count</span><span class="p">[</span><span class="n">el</span><span class="p">])]</span> <span class="o">=</span> <span class="n">i</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Otherwise we make a new, empty Atoms object. Atoms will</span>
            <span class="c1"># be ordered as they are in .castep file.</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="n">n_atoms</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>

        <span class="c1"># Now parse the positions, converting from units of Bohr</span>
        <span class="k">if</span> <span class="n">poslines</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">poslines</span><span class="p">):</span>
                <span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">arrow</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)</span> <span class="ow">in</span> <span class="n">lookup</span><span class="p">:</span>
                    <span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]</span> <span class="o">=</span> <span class="n">i</span>
                <span class="n">at</span><span class="o">.</span><span class="n">z</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="n">atomic_number</span><span class="p">(</span><span class="n">el</span><span class="p">)</span>
                <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">float</span><span class="p">(</span><span class="n">r</span><span class="p">)</span><span class="o">*</span> <span class="n">BOHR</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">)]</span>

            <span class="n">at</span><span class="o">.</span><span class="n">set_atoms</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">z</span><span class="p">)</span> <span class="c1"># set at.species property to match at.z</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span> <span class="n">at</span><span class="o">.</span><span class="n">frac_pos</span><span class="p">)</span>

        <span class="c1"># Velocities, if this is an MD file, from atomic units to A/fs</span>
        <span class="k">if</span> <span class="n">velolines</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;velo&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">velolines</span><span class="p">):</span>
                <span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">vx</span><span class="p">,</span> <span class="n">vy</span><span class="p">,</span> <span class="n">vz</span><span class="p">,</span> <span class="n">arrow</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
                <span class="n">at</span><span class="o">.</span><span class="n">velo</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">float</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">*</span><span class="n">BOHR</span><span class="o">/</span><span class="n">AU_FS</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">(</span><span class="n">vx</span><span class="p">,</span> <span class="n">vy</span><span class="p">,</span> <span class="n">vz</span><span class="p">)</span> <span class="p">]</span>

        <span class="c1"># And finally the forces, which are in units of Hartree/Bohr</span>
        <span class="k">if</span> <span class="n">forcelines</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">forcelines</span><span class="p">):</span>
                <span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">fx</span><span class="p">,</span> <span class="n">fy</span><span class="p">,</span> <span class="n">fz</span><span class="p">,</span> <span class="n">arrow</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
                <span class="n">at</span><span class="o">.</span><span class="n">force</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">float</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">*</span><span class="n">HARTREE</span><span class="o">/</span><span class="n">BOHR</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">(</span><span class="n">fx</span><span class="p">,</span> <span class="n">fy</span><span class="p">,</span> <span class="n">fz</span><span class="p">)</span> <span class="p">]</span>

        <span class="k">if</span> <span class="n">stress_lines</span><span class="p">:</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;virial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">virial</span><span class="o">*</span><span class="n">at</span><span class="o">.</span><span class="n">cell_volume</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">atoms_ref</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">yield</span> <span class="n">at</span>

<span class="c1"># Synonyms</span>
<span class="n">CastepGeomReader</span> <span class="o">=</span> <span class="n">CastepMDReader</span> <span class="o">=</span> <span class="n">CastepGeomMDReader</span>

<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;castep&#39;</span><span class="p">)</span>
<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;castep_log&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">CastepOutputReader</span><span class="p">(</span><span class="n">castep_file</span><span class="p">,</span> <span class="n">atoms_ref</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">abort</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Parse .castep file, and return Atoms object with positions,</span>
<span class="sd">       energy, forces, and possibly stress and atomic populations as</span>
<span class="sd">       well&quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">castep_file</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="n">castep_file_name</span> <span class="o">=</span> <span class="n">castep_file</span>
        <span class="n">castep_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">castep_file</span><span class="p">,</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">castep_file_name</span> <span class="o">=</span> <span class="s1">&#39;&lt;open file&gt;&#39;</span>
    <span class="n">castep_file</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">castep_file</span><span class="p">)</span>

    <span class="n">param</span> <span class="o">=</span> <span class="n">CastepParam</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">has_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">):</span>
        <span class="n">atoms_ref</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
        <span class="n">atoms_ref</span><span class="o">.</span><span class="n">frac_pos</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">atoms_ref</span><span class="o">.</span><span class="n">g</span><span class="p">,</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">pos</span><span class="p">)</span>

    <span class="n">got_header</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">eof</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>

        <span class="n">castep_output</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">line</span> <span class="o">=</span> <span class="n">castep_file</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
                <span class="n">eof</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">break</span>
            <span class="n">castep_output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; Atomic calculation performed&#39;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">got_header</span><span class="p">:</span>
                    <span class="n">got_param</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">got_header</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; Starting MD iteration&#39;</span><span class="p">):</span>
                <span class="k">break</span>

            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; Starting BFGS iteration&#39;</span><span class="p">):</span>
                <span class="k">break</span>

            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; BFGS: improving iteration&#39;</span><span class="p">):</span>
                <span class="k">break</span>

        <span class="k">if</span> <span class="n">castep_output</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">break</span>

        <span class="c1"># Remove newlines from end of each line in castep_output</span>
        <span class="n">castep_output</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">castep_output</span><span class="p">]</span>

        <span class="c1"># NB: CASTEP doesn&#39;t always print &#39;Total time&#39;</span>
        <span class="n">run_time</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">total_time</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Total time&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">total_time</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="n">has_converged</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Total energy has converged&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">abort</span> <span class="ow">and</span> <span class="n">has_converged</span> <span class="o">==</span> <span class="p">[]:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;castep didn&#39;t complete&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">run_time</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">total_time</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">3</span><span class="p">])</span>

        <span class="c1"># Now we should have contents of a valid .castep file in castep_output</span>

        <span class="c1"># First let&#39;s read the user parameters for this run from top of file</span>
        <span class="n">new_param</span> <span class="o">=</span> <span class="n">CastepParam</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">new_param</span><span class="o">.</span><span class="n">read_from_castep_output</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span>
            <span class="n">param</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">new_param</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="c1"># If iprint &lt; 2, params are not in .castep, file so let&#39;s look for .param file in same place</span>
            <span class="n">param_file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">castep_file_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;.param&#39;</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">param_file</span><span class="p">):</span>
                <span class="n">new_param</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">param_file</span><span class="p">)</span>
                <span class="n">param</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">new_param</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">abort</span><span class="p">:</span>
                <span class="k">raise</span>

        <span class="c1"># Next let&#39;s extract the lattice and atomic positions</span>
        <span class="n">lattice_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="s1">&#39;                                      Unit Cell&#39;</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">lattice_lines</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">abort</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No unit cell found in castep file - try passing atoms_ref&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">lattice</span> <span class="o">=</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">lattice_line</span> <span class="o">=</span> <span class="n">lattice_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># last lattice</span>

            <span class="n">lattice_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">lattice_line</span><span class="o">+</span><span class="mi">3</span><span class="p">:</span><span class="n">lattice_line</span><span class="o">+</span><span class="mi">6</span><span class="p">]</span>
            <span class="n">lattice</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
            <span class="n">lattice</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">lattice_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">])</span>
            <span class="n">lattice</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">lattice_lines</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">])</span>
            <span class="n">lattice</span><span class="p">[:,</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">lattice_lines</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">])</span>

        <span class="n">cell_contents</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="ow">in</span>  <span class="nb">enumerate</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="s1">&#39;                                     Cell Contents&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">cell_contents</span> <span class="o">==</span> <span class="p">[]:</span>
            <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">abort</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No cell contents found in castep file - try passing atoms_ref&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;No cell contents. If this is a variable cell geometry optimisation with fixed ions this is normal&#39;</span><span class="p">)</span>
                <span class="n">n_atoms</span> <span class="o">=</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">n</span>

        <span class="k">if</span> <span class="n">cell_contents</span><span class="p">:</span>
            <span class="n">cell_first_line</span> <span class="o">=</span> <span class="n">cell_contents</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># last cell contents line</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">n_atoms</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">castep_output</span><span class="p">[</span><span class="n">cell_first_line</span><span class="o">+</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">offset</span> <span class="o">=</span> <span class="mi">10</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="n">offset</span> <span class="o">=</span> <span class="mi">7</span>
                <span class="n">n_atoms</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">while</span> <span class="s1">&#39;xxxxxxxxxxx&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">cell_first_line</span><span class="o">+</span><span class="n">offset</span><span class="o">+</span><span class="n">n_atoms</span><span class="p">]:</span>
                   <span class="n">n_atoms</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># If we were passed in an Atoms object, construct mapping from</span>
            <span class="c1"># CASTEP (element, number) to original atom index</span>
            <span class="k">assert</span> <span class="n">n_atoms</span> <span class="o">==</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">n</span><span class="p">,</span> <span class="s1">&#39;Number of atoms must match atoms_ref&#39;</span>
            <span class="n">atoms</span> <span class="o">=</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

            <span class="c1"># remove things which shouldn&#39;t be inherited from atoms_ref</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">,</span> <span class="s1">&#39;enthalpy&#39;</span><span class="p">,</span> <span class="s1">&#39;virial&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">atoms</span><span class="o">.</span><span class="n">params</span><span class="p">:</span> <span class="k">del</span> <span class="n">atoms</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">p</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_s&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_p&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_d&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_f&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_total&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_charge&#39;</span><span class="p">,</span> <span class="s1">&#39;popn_spin&#39;</span><span class="p">]:</span>
                <span class="k">if</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">:</span> <span class="k">del</span> <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="n">p</span><span class="p">]</span>

            <span class="n">atoms</span><span class="o">.</span><span class="n">set_lattice</span><span class="p">(</span><span class="n">lattice</span><span class="p">,</span> <span class="n">scale_positions</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">species_count</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">atoms</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="n">el</span> <span class="o">=</span> <span class="n">atoms</span><span class="o">.</span><span class="n">species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">species_count</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">el</span><span class="p">):</span>
                    <span class="n">species_count</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">species_count</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">species_count</span><span class="p">[</span><span class="n">el</span><span class="p">])]</span> <span class="o">=</span> <span class="n">i</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Otherwise we make a new, empty Atoms object. Atoms will</span>
            <span class="c1"># be ordered as they are in .castep file.</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">atoms</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="n">n_atoms</span><span class="p">,</span><span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">cell_contents</span><span class="p">:</span>
            <span class="n">cell_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">cell_first_line</span><span class="o">+</span><span class="n">offset</span><span class="p">:</span><span class="n">cell_first_line</span><span class="o">+</span><span class="n">offset</span><span class="o">+</span><span class="n">n_atoms</span><span class="p">]</span>

            <span class="c1"># Fill in species and fractional positions</span>
            <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">,</span><span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">cell_lines</span><span class="p">):</span>
                <span class="n">x1</span><span class="p">,</span> <span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">x2</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)</span> <span class="ow">in</span> <span class="n">lookup</span><span class="p">:</span>
                    <span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]</span> <span class="o">=</span> <span class="n">i</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">z</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="n">atomic_number</span><span class="p">(</span><span class="n">el</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">frac_pos</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="n">v</span><span class="p">,</span><span class="n">w</span><span class="p">))</span>

            <span class="n">atoms</span><span class="o">.</span><span class="n">set_atoms</span><span class="p">(</span><span class="n">atoms</span><span class="o">.</span><span class="n">z</span><span class="p">)</span> <span class="c1"># set at.species from at.z</span>

        <span class="c1"># Calculate cartesian postions from fractional positions</span>
        <span class="c1"># (correct if we&#39;re doing a variable cell geom. opt. with fixed ions)</span>
        <span class="n">atoms</span><span class="o">.</span><span class="n">pos</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">atoms</span><span class="o">.</span><span class="n">lattice</span><span class="p">,</span> <span class="n">atoms</span><span class="o">.</span><span class="n">frac_pos</span><span class="p">)</span>

        <span class="n">energy_lines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Final energy&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">s</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&lt;- EDFT&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">)</span>

        <span class="c1"># If we&#39;re using smearing, correct energy is &#39;free energy&#39;</span>
        <span class="n">energy_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Final free energy (E-TS)&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">))</span>

        <span class="c1"># How about the dispersion correction?</span>
        <span class="n">energy_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Dispersion corrected final energy&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">))</span>
        <span class="n">energy_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Dispersion corrected final free energy&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">))</span>

        <span class="c1"># Are we doing finite basis correction?</span>
        <span class="n">energy_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; Total energy corrected for finite basis set&#39;</span><span class="p">),</span> <span class="n">castep_output</span><span class="p">))</span>

        <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">energy_lines</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No total energy found in castep file&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Use last matching energy line in file</span>
            <span class="c1"># Has this energy been corrected for the finite basis set?</span>
            <span class="n">energy_param_name</span> <span class="o">=</span> <span class="s1">&#39;energy&#39;</span>
            <span class="k">if</span> <span class="s1">&#39;not corrected for finite basis set&#39;</span> <span class="ow">in</span> <span class="n">energy_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span> <span class="n">energy_param_name</span> <span class="o">=</span> <span class="s1">&#39;energy_no_basis_corr&#39;</span>

            <span class="n">fields</span> <span class="o">=</span> <span class="n">energy_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="k">if</span> <span class="s1">&#39;eV&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No value found in energy line &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="n">energy_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">energy_param_name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="n">fields</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;eV&#39;</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
            <span class="c1"># If we&#39;re doing a dispersion correction, report that as well</span>
            <span class="c1"># Take care not to use the final basis-set-corrected figure --</span>
            <span class="c1"># that correction is added last!</span>
            <span class="n">esedc_param_name</span> <span class="o">=</span> <span class="s1">&#39;energy_disp_corr&#39;</span>
            <span class="n">energy_lines_base</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">energy_lines</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Dispersion&#39;</span><span class="p">)</span> <span class="ow">or</span>
                        <span class="s1">&#39;corrected for finite basis set&#39;</span> <span class="ow">in</span> <span class="n">line</span><span class="p">)]</span>
            <span class="n">energy_lines_disp</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">energy_lines</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Dispersion&#39;</span><span class="p">)</span>
                   <span class="ow">and</span> <span class="s1">&#39;corrected for finite basis set&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">line</span><span class="p">]</span>
            <span class="k">def</span> <span class="nf">parse_eline</span><span class="p">(</span><span class="n">eline</span><span class="p">):</span>
                <span class="n">fields</span> <span class="o">=</span> <span class="n">eline</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="k">if</span> <span class="s1">&#39;eV&#39;</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
                    <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="n">fields</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;eV&#39;</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                            <span class="s1">&#39;No value found in energy line &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="n">eline</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">return</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">energy_lines_disp</span> <span class="ow">and</span> <span class="n">energy_lines_base</span><span class="p">:</span>
                <span class="n">ebase</span> <span class="o">=</span> <span class="n">parse_eline</span><span class="p">(</span><span class="n">energy_lines_base</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">edisp</span> <span class="o">=</span> <span class="n">parse_eline</span><span class="p">(</span><span class="n">energy_lines_disp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">esedc_param_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">edisp</span> <span class="o">-</span> <span class="n">ebase</span>

        <span class="c1"># If we&#39;re doing geom-opt, look for enthalpy</span>
        <span class="n">enthalpy_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">castep_output</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; BFGS: finished iteration &#39;</span><span class="p">)</span> <span class="ow">or</span>
                          <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; BFGS: Final Enthalpy&#39;</span><span class="p">)]</span>
        <span class="k">if</span> <span class="n">enthalpy_lines</span><span class="p">:</span>
            <span class="n">atoms</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;enthalpy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">enthalpy_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>

        <span class="k">try</span><span class="p">:</span>

            <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;Forces&#39;</span><span class="p">,</span> <span class="s1">&#39;Symmetrised Forces&#39;</span><span class="p">):</span>
                <span class="n">force_start_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">s</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;****** </span><span class="si">%s</span><span class="s1"> ******&#39;</span> <span class="o">%</span> <span class="n">fn</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">force_start_lines</span><span class="p">:</span> <span class="k">break</span>

            <span class="k">if</span> <span class="n">force_start_lines</span> <span class="o">==</span> <span class="p">[]:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span>

            <span class="c1"># Use last set of forces</span>
            <span class="n">force_start</span> <span class="o">=</span> <span class="n">force_start_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

            <span class="c1"># Extract force lines from .castep file</span>
            <span class="n">force_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">force_start</span><span class="o">+</span><span class="mi">6</span><span class="p">:</span><span class="n">force_start</span><span class="o">+</span><span class="mi">6</span><span class="o">+</span><span class="n">atoms</span><span class="o">.</span><span class="n">n</span><span class="p">]</span>

            <span class="c1"># remove &quot;cons&quot; tags from constrained degrees of freedom</span>
            <span class="n">force_lines</span> <span class="o">=</span> <span class="p">[</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;(cons&#39; d)&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">force_lines</span> <span class="p">]</span>

            <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">,</span><span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

            <span class="c1"># Fill in the forces</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">force_lines</span><span class="p">):</span>
                <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="c1"># Remove the *s</span>
                <span class="n">el</span><span class="p">,</span> <span class="n">num_str</span><span class="p">,</span> <span class="n">fx</span><span class="p">,</span> <span class="n">fy</span><span class="p">,</span> <span class="n">fz</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num_str</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">force</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="p">(</span><span class="n">fx</span><span class="p">,</span><span class="n">fy</span><span class="p">,</span><span class="n">fz</span><span class="p">)</span>

        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">,</span> <span class="n">m</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No forces found in castep file </span><span class="si">%s</span><span class="s1">: &#39;</span> <span class="o">%</span> <span class="n">m</span><span class="p">)</span>

        <span class="c1"># Individual contributions to total force</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span><span class="n">label</span> <span class="ow">in</span> <span class="p">[(</span><span class="s1">&#39;force_ewald&#39;</span><span class="p">,</span> <span class="s1">&#39;Ewald forces&#39;</span><span class="p">),</span>
                           <span class="p">(</span><span class="s1">&#39;force_locpot&#39;</span><span class="p">,</span> <span class="s1">&#39;Local potential forces&#39;</span><span class="p">),</span>
                           <span class="p">(</span><span class="s1">&#39;force_nlpot&#39;</span><span class="p">,</span> <span class="s1">&#39;Non-local potential forces&#39;</span><span class="p">),</span>
                           <span class="p">(</span><span class="s1">&#39;force_extpot&#39;</span><span class="p">,</span> <span class="s1">&#39;External potential forces&#39;</span><span class="p">),</span>
                           <span class="p">(</span><span class="s1">&#39;force_sedc&#39;</span><span class="p">,</span> <span class="s1">&#39;DFTD sedc forces&#39;</span><span class="p">)]:</span>
            <span class="n">force_start_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">s</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;****** </span><span class="si">%s</span><span class="s1"> ******&#39;</span> <span class="o">%</span> <span class="n">label</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">force_start_lines</span> <span class="o">==</span> <span class="p">[]:</span> <span class="k">continue</span>

            <span class="c1"># Use last set of forces</span>
            <span class="n">force_start</span> <span class="o">=</span> <span class="n">force_start_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

            <span class="c1"># Extract force lines from .castep file</span>
            <span class="n">force_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">force_start</span><span class="o">+</span><span class="mi">6</span><span class="p">:</span><span class="n">force_start</span><span class="o">+</span><span class="mi">6</span><span class="o">+</span><span class="n">atoms</span><span class="o">.</span><span class="n">n</span><span class="p">]</span>

            <span class="c1"># remove &quot;cons&quot; tags from constrained degrees of freedom</span>
            <span class="n">force_lines</span> <span class="o">=</span> <span class="p">[</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;(cons&#39; d)&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">force_lines</span> <span class="p">]</span>

            <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="mf">0.0</span><span class="p">,</span><span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

            <span class="c1"># Fill in the forces</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">force_lines</span><span class="p">):</span>
                <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="c1"># Remove the *s</span>
                <span class="n">el</span><span class="p">,</span> <span class="n">num_str</span><span class="p">,</span> <span class="n">fx</span><span class="p">,</span> <span class="n">fy</span><span class="p">,</span> <span class="n">fz</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num_str</span><span class="p">)</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">atoms</span><span class="p">,</span> <span class="n">name</span><span class="p">)[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="p">(</span><span class="n">fx</span><span class="p">,</span><span class="n">fy</span><span class="p">,</span><span class="n">fz</span><span class="p">)</span>

        <span class="c1"># Have we calculated stress?</span>
        <span class="n">got_virial</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">sn</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;Stress Tensor&#39;</span><span class="p">,</span> <span class="s1">&#39;Symmetrised Stress Tensor&#39;</span><span class="p">):</span>
            <span class="n">stress_start_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">s</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;****** </span><span class="si">%s</span><span class="s1"> ******&#39;</span> <span class="o">%</span> <span class="n">sn</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="p">]</span>
            <span class="k">if</span> <span class="n">stress_start_lines</span><span class="p">:</span> <span class="k">break</span>

        <span class="k">if</span> <span class="n">stress_start_lines</span><span class="p">:</span>
            <span class="n">stress_start</span> <span class="o">=</span> <span class="n">stress_start_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">stress_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">stress_start</span><span class="o">+</span><span class="mi">6</span><span class="p">:</span><span class="n">stress_start</span><span class="o">+</span><span class="mi">9</span><span class="p">]</span>
            <span class="n">virial</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span><span class="nb">float</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">stress_lines</span><span class="p">):</span>
                <span class="n">star1</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">vx</span><span class="p">,</span> <span class="n">vy</span><span class="p">,</span> <span class="n">vz</span><span class="p">,</span> <span class="n">star2</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">virial</span><span class="p">[:,</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="p">(</span><span class="n">vx</span><span class="p">,</span><span class="n">vy</span><span class="p">,</span><span class="n">vz</span><span class="p">)</span> <span class="p">]</span>
            <span class="n">got_virial</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="n">spin_polarised</span> <span class="o">=</span> <span class="s1">&#39;spin_polarised&#39;</span> <span class="ow">in</span> <span class="n">param</span> <span class="ow">and</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;spin_polarised&#39;</span><span class="p">]</span>

        <span class="c1"># Have we calculated local populations and charges?</span>
        <span class="k">if</span> <span class="s1">&#39;popn_calculate&#39;</span> <span class="ow">in</span> <span class="n">param</span> <span class="ow">and</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;popn_calculate&#39;</span><span class="p">]:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">popn_start</span> <span class="o">=</span> <span class="n">castep_output</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;     Atomic Populations&#39;</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">popn_start</span> <span class="o">=</span> <span class="n">castep_output</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;     Atomic Populations (Mulliken)&#39;</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="k">raise</span>

                <span class="n">popn_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">popn_start</span><span class="o">+</span><span class="mi">4</span><span class="p">:</span><span class="n">popn_start</span><span class="o">+</span><span class="mi">4</span><span class="o">+</span><span class="n">atoms</span><span class="o">.</span><span class="n">n</span><span class="p">]</span>

                <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_s&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_p&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_d&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_f&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_total&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_charge&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">spin_polarised</span><span class="p">:</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;popn_spin&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>

                <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">popn_lines</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">spin_polarised</span><span class="p">:</span>
                        <span class="n">el</span><span class="p">,</span> <span class="n">num_str</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">tot</span><span class="p">,</span> <span class="n">charge</span><span class="p">,</span> <span class="n">spin</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">el</span><span class="p">,</span> <span class="n">num_str</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">tot</span><span class="p">,</span> <span class="n">charge</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                    <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num_str</span><span class="p">)</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">popn_s</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">popn_p</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">popn_d</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">popn_f</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">popn_total</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">tot</span><span class="p">)</span>
                    <span class="n">atoms</span><span class="o">.</span><span class="n">popn_charge</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">charge</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">spin_polarised</span><span class="p">:</span>
                        <span class="n">atoms</span><span class="o">.</span><span class="n">popn_spin</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span><span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">spin</span><span class="p">)</span>

            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No populations found in castep file&#39;</span><span class="p">)</span>

        <span class="c1"># Also try Hirshfeld population analysis</span>
        <span class="k">if</span> <span class="s1">&#39;calculate_hirshfeld&#39;</span> <span class="ow">in</span> <span class="n">param</span> <span class="ow">and</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;calculate_hirshfeld&#39;</span><span class="p">]:</span>
           <span class="k">try</span><span class="p">:</span>
              <span class="n">hirshfeld_basic_start</span> <span class="o">=</span> <span class="n">castep_output</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;     Hirshfeld Analysis&#39;</span><span class="p">)</span>
           <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
              <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                 <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;No Hirshfeld charges in castep file&#39;</span><span class="p">)</span>
           <span class="n">hirshfeld_basic_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">hirshfeld_basic_start</span><span class="o">+</span><span class="mi">4</span><span class="p">:</span><span class="n">hirshfeld_basic_start</span><span class="o">+</span><span class="mi">4</span><span class="o">+</span><span class="n">atoms</span><span class="o">.</span><span class="n">n</span><span class="p">]</span>
           <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;hirshfeld_charge&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
           <span class="k">if</span> <span class="n">spin_polarised</span><span class="p">:</span>
               <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;hirshfeld_spin&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
           <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">hirshfeld_basic_lines</span><span class="p">:</span>
               <span class="n">fields</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
               <span class="k">try</span><span class="p">:</span>
                   <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
                       <span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">charge</span><span class="p">,</span> <span class="n">spin</span> <span class="o">=</span> <span class="n">fields</span>
                       <span class="n">spin</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">spin</span><span class="p">)</span>
                   <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                       <span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">charge</span> <span class="o">=</span> <span class="n">fields</span>
                       <span class="n">spin</span> <span class="o">=</span> <span class="mf">0.0</span>
                   <span class="k">else</span><span class="p">:</span>
                       <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">()</span>
                   <span class="n">num</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
                   <span class="n">charge</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">charge</span><span class="p">)</span>
               <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                   <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                       <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unable to parse Hirshfeld charge line &quot;</span><span class="si">{}</span><span class="s1">&quot;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">line</span><span class="p">))</span>
               <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="s1">&#39;hirshfeld_charge&#39;</span><span class="p">][</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="n">charge</span>
               <span class="k">if</span> <span class="n">spin_polarised</span><span class="p">:</span>
                   <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="s1">&#39;hirshfeld_spin&#39;</span><span class="p">][</span><span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span> <span class="n">num</span><span class="p">)]]</span> <span class="o">=</span> <span class="n">spin</span>
           <span class="c1">#TODO get the dipole moment as well?</span>

           <span class="c1"># If iprint &gt;= 2, get additional info that is useful for dispersion corrections</span>
           <span class="n">hirshfeld_extra_start</span> <span class="o">=</span> <span class="n">hirshfeld_basic_start</span>
           <span class="k">for</span> <span class="n">hirshfeld_extra_start</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">hirshfeld_basic_start</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
               <span class="k">if</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">hirshfeld_extra_start</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; *************&#39;</span><span class="p">):</span>
                  <span class="k">break</span>
           <span class="n">hirshfeld_extra_lines</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">hirshfeld_extra_start</span><span class="o">+</span><span class="mi">1</span><span class="p">:</span><span class="n">hirshfeld_basic_start</span><span class="p">]</span>
           <span class="n">hirshfeld_extra_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">hirshfeld_extra_lines</span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()]</span>
           <span class="k">if</span> <span class="n">hirshfeld_extra_lines</span><span class="p">:</span>
               <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;hirshfeld_volume&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
               <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;hirshfeld_rel_volume&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
               <span class="n">hirsh_extra_blocks</span> <span class="o">=</span> <span class="p">[]</span>
               <span class="c1"># The species match in this regex might seem excessive,</span>
               <span class="c1"># but you never know when you&#39;ll want to do DFT on Uuq!</span>
               <span class="c1"># (wait, now its symbol is Fl - oh, whatever.)</span>
               <span class="n">species_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;\s+Species\s+(\d+),\s+Atom\s+(\d+)\s+:\s+([A-Z][a-z]{0,2})&#39;</span><span class="p">)</span>
               <span class="n">parse_charge</span> <span class="o">=</span> <span class="kc">False</span>
               <span class="n">parse_vol</span> <span class="o">=</span> <span class="kc">False</span>
               <span class="n">parse_rel_vol</span> <span class="o">=</span> <span class="kc">False</span>
           <span class="n">atom_idx</span> <span class="o">=</span> <span class="mi">0</span>
           <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">hirshfeld_extra_lines</span><span class="p">:</span>
               <span class="k">if</span> <span class="n">parse_charge</span><span class="p">:</span>
                   <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="s1">&#39;hirshfeld_charge&#39;</span><span class="p">][</span><span class="n">atom_idx</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
                   <span class="n">parse_charge</span> <span class="o">=</span> <span class="kc">False</span>
                   <span class="k">continue</span>
               <span class="k">if</span> <span class="n">parse_vol</span><span class="p">:</span>
                   <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="s1">&#39;hirshfeld_volume&#39;</span><span class="p">][</span><span class="n">atom_idx</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
                   <span class="n">parse_vol</span> <span class="o">=</span> <span class="kc">False</span>
                   <span class="k">continue</span>
               <span class="k">if</span> <span class="n">parse_rel_vol</span><span class="p">:</span>
                   <span class="n">atoms</span><span class="o">.</span><span class="n">properties</span><span class="p">[</span><span class="s1">&#39;hirshfeld_rel_volume&#39;</span><span class="p">][</span><span class="n">atom_idx</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
                   <span class="n">parse_rel_vol</span> <span class="o">=</span> <span class="kc">False</span>
                   <span class="k">continue</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39; Species&#39;</span><span class="p">):</span>
                   <span class="n">atom_line</span> <span class="o">=</span> <span class="n">species_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                   <span class="n">num</span><span class="p">,</span> <span class="n">el</span> <span class="o">=</span> <span class="n">atom_line</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
                   <span class="n">atom_idx</span> <span class="o">=</span> <span class="n">lookup</span><span class="p">[(</span><span class="n">el</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">))]</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Hirshfeld net atomic charge&#39;</span><span class="p">):</span>
                   <span class="n">parse_charge</span> <span class="o">=</span> <span class="kc">True</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Hirshfeld atomic volume&#39;</span><span class="p">):</span>
                   <span class="n">parse_vol</span> <span class="o">=</span> <span class="kc">True</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Hirshfeld / free atomic volume&#39;</span><span class="p">):</span>
                   <span class="n">parse_rel_vol</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="n">mod_param</span> <span class="o">=</span> <span class="n">param</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="c1"># check for calculation of dipole moment - one vector per spin component</span>
        <span class="k">if</span> <span class="s1">&#39; calculate electric dipole moment of system     : on&#39;</span> <span class="ow">in</span> <span class="n">castep_output</span><span class="p">:</span>
            <span class="n">dipole_magnitudes</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">castep_output</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;  +  Magnitude of Dipole&#39;</span><span class="p">)]</span>
            <span class="n">dipole_directions</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">castep_output</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;  +  Direction of Dipole&#39;</span><span class="p">)]</span>

            <span class="n">dipoles</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">mag_line</span><span class="p">,</span> <span class="n">dir_line</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">dipole_magnitudes</span><span class="p">,</span> <span class="n">dipole_directions</span><span class="p">):</span>
                <span class="n">magnitude</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">mag_line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">5</span><span class="p">])</span><span class="o">*</span><span class="n">DEBYE</span>
                <span class="n">direction</span> <span class="o">=</span> <span class="n">farray</span><span class="p">([</span><span class="nb">float</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">dir_line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">5</span><span class="p">:</span><span class="mi">8</span><span class="p">]])</span>
                <span class="n">dipoles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">magnitude</span><span class="o">*</span><span class="n">direction</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">dipole</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">dipoles</span><span class="p">):</span>
                <span class="n">mod_param</span><span class="p">[</span><span class="s1">&#39;dipole</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">dipole</span>


        <span class="c1"># append K-point information</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">kpoint_start</span> <span class="o">=</span> <span class="n">castep_output</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;                              k-Points For BZ Sampling&#39;</span><span class="p">)</span>
            <span class="n">kp_mesh_line</span> <span class="o">=</span> <span class="n">castep_output</span><span class="p">[</span><span class="n">kpoint_start</span><span class="o">+</span><span class="mi">2</span><span class="p">]</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">kp_mesh_line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="n">mod_param</span><span class="p">[</span><span class="s1">&#39;kpoints_mp_grid&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">]),</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]),</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])]</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">pass</span>

        <span class="n">mod_param</span><span class="p">[</span><span class="s1">&#39;castep_file_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">castep_file_name</span>

        <span class="k">if</span> <span class="n">run_time</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">mod_param</span><span class="p">[</span><span class="s1">&#39;castep_run_time&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">run_time</span>

        <span class="c1"># Convert virial to libAtoms units and add to params</span>
        <span class="k">if</span> <span class="n">got_virial</span><span class="p">:</span>
            <span class="n">mod_param</span><span class="p">[</span><span class="s1">&#39;virial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">virial</span><span class="o">*</span><span class="n">atoms</span><span class="o">.</span><span class="n">cell_volume</span><span class="p">()</span><span class="o">/</span><span class="n">EV_A3_IN_GPA</span>

        <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">atoms_ref</span> <span class="o">=</span> <span class="n">atoms</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="n">atoms</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">mod_param</span><span class="p">)</span>

        <span class="k">yield</span> <span class="n">atoms</span>

        <span class="k">if</span> <span class="n">eof</span><span class="p">:</span>
            <span class="k">break</span>

<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;magres&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">MagresReader</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">atoms_ref</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generator to read .magres files. The specification of the format my be found at http://www.ccpnc.ac.uk/&quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">source</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="n">source</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">source</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">atoms_ref</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">atoms_ref</span><span class="o">.</span><span class="n">has_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">):</span>
        <span class="n">atoms_ref</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;frac_pos&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">,</span><span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
        <span class="n">atoms_ref</span><span class="o">.</span><span class="n">frac_pos</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">atoms_ref</span><span class="o">.</span><span class="n">g</span><span class="p">,</span><span class="n">atoms_ref</span><span class="o">.</span><span class="n">pos</span><span class="p">)</span>

    <span class="n">eof</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">parsing_started</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
       <span class="n">block</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
       <span class="n">atom_line</span> <span class="o">=</span> <span class="p">[]</span>
       <span class="n">ms_line</span> <span class="o">=</span> <span class="p">[]</span>
       <span class="n">efg_line</span> <span class="o">=</span> <span class="p">[]</span>
       <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
           <span class="k">try</span><span class="p">:</span>
               <span class="n">line</span><span class="o">=</span><span class="n">source</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
           <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
               <span class="n">eof</span> <span class="o">=</span> <span class="kc">True</span>
               
           <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;#\$magres&quot;</span><span class="p">,</span><span class="n">line</span><span class="p">)</span> <span class="ow">or</span> <span class="n">eof</span><span class="p">:</span>
               <span class="k">if</span> <span class="n">parsing_started</span><span class="p">:</span>
                  <span class="k">break</span>
               <span class="k">else</span><span class="p">:</span>
                  <span class="n">parsing_started</span> <span class="o">=</span> <span class="kc">True</span>
               <span class="k">continue</span>
           <span class="k">elif</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;\[[^/].*\]&quot;</span><span class="p">,</span><span class="n">line</span><span class="p">):</span>
               <span class="n">block</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;[&quot;</span><span class="p">,</span><span class="s2">&quot;&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;]&quot;</span><span class="p">,</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
               <span class="k">continue</span>
           <span class="k">elif</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;\[\/.*\]&quot;</span><span class="p">,</span><span class="n">line</span><span class="p">):</span>
               <span class="n">block</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
               <span class="k">continue</span>

           <span class="k">if</span> <span class="s2">&quot;atoms&quot;</span> <span class="ow">in</span> <span class="n">block</span><span class="p">:</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;lattice&quot;</span><span class="p">):</span>
                   <span class="n">lattice_line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
               <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;atom&quot;</span><span class="p">):</span>
                   <span class="n">atom_line</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
           <span class="k">if</span> <span class="s2">&quot;magres&quot;</span> <span class="ow">in</span> <span class="n">block</span><span class="p">:</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;ms&quot;</span><span class="p">):</span>
                   <span class="n">ms_line</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
               <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;efg&quot;</span><span class="p">):</span>
                   <span class="n">efg_line</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
       
       <span class="n">lattice</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
       <span class="n">lattice</span><span class="p">[:,:]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span><span class="n">lattice_line</span><span class="p">),(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>

       <span class="n">n_atoms</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">atom_line</span><span class="p">)</span>
       <span class="n">atoms</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="n">n_atoms</span><span class="p">,</span><span class="n">lattice</span><span class="o">=</span><span class="n">lattice</span><span class="p">)</span>
       <span class="n">lookup</span> <span class="o">=</span> <span class="p">{}</span>

       <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">atom_line</span><span class="p">):</span>
           <span class="n">label</span><span class="p">,</span> <span class="n">atom_type</span><span class="p">,</span> <span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">line</span>
           <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">)</span> <span class="ow">in</span> <span class="n">lookup</span><span class="p">:</span>
               <span class="n">lookup</span><span class="p">[(</span><span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">)]</span> <span class="o">=</span> <span class="n">i</span>
           <span class="n">atoms</span><span class="o">.</span><span class="n">z</span><span class="p">[</span><span class="n">lookup</span><span class="p">[(</span><span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">)]]</span> <span class="o">=</span> <span class="n">atomic_number</span><span class="p">(</span><span class="n">atom_type</span><span class="p">)</span>
           <span class="n">atoms</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">))</span>

       <span class="n">atoms</span><span class="o">.</span><span class="n">set_atoms</span><span class="p">(</span><span class="n">atoms</span><span class="o">.</span><span class="n">z</span><span class="p">)</span> <span class="c1"># set at.species from at.z</span>

       <span class="k">if</span> <span class="n">n_atoms</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">ms_line</span><span class="p">):</span>
           <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;ms&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">,</span><span class="n">n_cols</span><span class="o">=</span><span class="mi">9</span><span class="p">)</span>
           <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">ms_line</span><span class="p">):</span>
               <span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">,</span>
                <span class="n">ms_xx</span><span class="p">,</span> <span class="n">ms_xy</span><span class="p">,</span> <span class="n">ms_xz</span><span class="p">,</span>
                <span class="n">ms_yx</span><span class="p">,</span> <span class="n">ms_yy</span><span class="p">,</span> <span class="n">ms_yz</span><span class="p">,</span>
                <span class="n">ms_zx</span><span class="p">,</span> <span class="n">ms_zy</span><span class="p">,</span> <span class="n">ms_zz</span><span class="p">)</span> <span class="o">=</span> <span class="n">line</span>
               <span class="n">atoms</span><span class="o">.</span><span class="n">ms</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,(</span><span class="n">ms_xx</span><span class="p">,</span> <span class="n">ms_xy</span><span class="p">,</span> <span class="n">ms_xz</span><span class="p">,</span>
                                                                                         <span class="n">ms_yx</span><span class="p">,</span> <span class="n">ms_yy</span><span class="p">,</span> <span class="n">ms_yz</span><span class="p">,</span>
                                                                                         <span class="n">ms_zx</span><span class="p">,</span> <span class="n">ms_zy</span><span class="p">,</span> <span class="n">ms_zz</span><span class="p">))</span>
       <span class="k">if</span> <span class="n">n_atoms</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">efg_line</span><span class="p">):</span>
           <span class="n">atoms</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;efg&#39;</span><span class="p">,</span><span class="mf">0.0</span><span class="p">,</span><span class="n">n_cols</span><span class="o">=</span><span class="mi">9</span><span class="p">)</span>
           <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fenumerate</span><span class="p">(</span><span class="n">efg_line</span><span class="p">):</span>
               <span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">,</span>
                <span class="n">efg_xx</span><span class="p">,</span> <span class="n">efg_xy</span><span class="p">,</span> <span class="n">efg_xz</span><span class="p">,</span>
                <span class="n">efg_yx</span><span class="p">,</span> <span class="n">efg_yy</span><span class="p">,</span> <span class="n">efg_yz</span><span class="p">,</span>
                <span class="n">efg_zx</span><span class="p">,</span> <span class="n">efg_zy</span><span class="p">,</span> <span class="n">efg_zz</span><span class="p">)</span> <span class="o">=</span> <span class="n">line</span>
               <span class="n">atoms</span><span class="o">.</span><span class="n">efg</span><span class="p">[:,</span><span class="n">lookup</span><span class="p">[(</span><span class="n">species_label</span><span class="p">,</span> <span class="n">number_in_species_label</span><span class="p">)]]</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,(</span><span class="n">efg_xx</span><span class="p">,</span> <span class="n">efg_xy</span><span class="p">,</span> <span class="n">efg_xz</span><span class="p">,</span>
                                                                                         <span class="n">efg_yx</span><span class="p">,</span> <span class="n">efg_yy</span><span class="p">,</span> <span class="n">efg_yz</span><span class="p">,</span>
                                                                                         <span class="n">efg_zx</span><span class="p">,</span> <span class="n">efg_zy</span><span class="p">,</span> <span class="n">efg_zz</span><span class="p">))</span>

       <span class="k">yield</span> <span class="n">atoms</span>
       <span class="k">if</span> <span class="n">eof</span><span class="p">:</span>
          <span class="k">break</span>

<span class="k">def</span> <span class="nf">get_valid_keywords</span><span class="p">(</span><span class="n">castep</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Determines valid cell and parameter keyword by invoking castep with -help parameter.</span>
<span class="sd">       Returns a tuple (valid_cell_keywords, valid_parameters_keywords)&quot;&quot;&quot;</span>

    <span class="n">valid_cell_keywords</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">valid_parameters_keywords</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">if</span> <span class="n">castep</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
        <span class="n">castep</span> <span class="o">=</span> <span class="n">castep</span> <span class="o">+</span> <span class="s1">&#39; </span><span class="si">%s</span><span class="s1">&#39;</span>

    <span class="n">lines</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">castep</span> <span class="o">%</span> <span class="p">(</span><span class="s1">&#39;-help all&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
       <span class="n">cell_start</span> <span class="o">=</span> <span class="n">lines</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;Help information on CELL keywords:</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
       <span class="n">param_start</span> <span class="o">=</span> <span class="n">lines</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;Help information on PARAMETERS keywords:</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
       <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Error parsing output of castep -help all&#39;</span><span class="p">)</span>

    <span class="n">cell_lines</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">cell_start</span><span class="o">+</span><span class="mi">2</span><span class="p">:</span><span class="n">param_start</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
    <span class="n">param_lines</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">param_start</span><span class="o">+</span><span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

    <span class="k">for</span> <span class="n">lines</span><span class="p">,</span> <span class="n">keywords</span>  <span class="ow">in</span> <span class="nb">zip</span><span class="p">((</span><span class="n">cell_lines</span><span class="p">,</span> <span class="n">param_lines</span><span class="p">),</span>
          <span class="p">(</span><span class="n">valid_cell_keywords</span><span class="p">,</span><span class="n">valid_parameters_keywords</span><span class="p">)):</span>
       <span class="n">lines</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">strip</span><span class="p">,</span> <span class="n">lines</span><span class="p">)</span>
       <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
          <span class="n">keywords</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>

    <span class="nb">print</span> <span class="s1">&#39;valid_cell_keywords = </span><span class="si">%r</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">valid_cell_keywords</span>
    <span class="nb">print</span> <span class="s1">&#39;valid_parameters_keywords = </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">valid_parameters_keywords</span>

    <span class="k">return</span> <span class="p">(</span><span class="n">valid_cell_keywords</span><span class="p">,</span> <span class="n">valid_parameters_keywords</span><span class="p">)</span>


<div class="viewcode-block" id="check_pspots"><a class="viewcode-back" href="../../io.html#quippy.castep.check_pspots">[docs]</a><span class="k">def</span> <span class="nf">check_pspots</span><span class="p">(</span><span class="n">cluster</span><span class="p">,</span> <span class="n">cell</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">orig_dir</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Check pseudopotential files are present, and that we have one for</span>
<span class="sd">    each element present in cluster. Also tries to check spin polarisation</span>
<span class="sd">    of system matches that specified in parameters.&quot;&quot;&quot;</span>

    <span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;castep_driver&#39;</span><span class="p">)</span>

    <span class="c1"># Get list of unique elements in cluster</span>
    <span class="n">pspot_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">cluster</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">():</span>
        <span class="n">pspot_dict</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">elements</span> <span class="o">=</span> <span class="n">pspot_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">elements</span><span class="p">))</span>

    <span class="k">if</span> <span class="s1">&#39;SPECIES_POT&#39;</span> <span class="ow">in</span> <span class="n">cell</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">cell</span><span class="p">[</span><span class="s1">&#39;SPECIES_POT&#39;</span><span class="p">]:</span>
            <span class="n">element</span><span class="p">,</span> <span class="n">pspot</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">strip</span><span class="p">,</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
            <span class="n">pspot_dict</span><span class="p">[</span><span class="n">element</span><span class="p">]</span> <span class="o">=</span> <span class="n">pspot</span>

    <span class="n">valence_charges</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">missing_pspots</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">elements</span><span class="p">:</span>
        <span class="n">pspot</span> <span class="o">=</span> <span class="n">pspot_dict</span><span class="p">[</span><span class="n">el</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">pspot</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Not fatal since on-the-fly pseudo-potential will be generated</span>
            <span class="c1"># however, we can&#39;t estimate then n_electrons here</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Pseudopotential missing for element </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">el</span><span class="p">)</span>
            <span class="n">valence_charges</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;?&#39;</span>
            <span class="n">missing_pspots</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">pspot</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;/&quot;&#39;</span><span class="p">):</span>
            <span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">orig_dir</span><span class="o">+</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="n">pspot</span><span class="p">,</span> <span class="s1">&#39;.&#39;</span><span class="p">)</span>
            <span class="n">pspot_lines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">pspot</span><span class="p">,</span><span class="s1">&#39;r&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

            <span class="c1"># First check for old-style pspot report</span>
            <span class="n">zlines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;    |  z =&#39;</span><span class="p">),</span> <span class="n">pspot_lines</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">zlines</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="c1"># Extract number of valence electrons</span>
                <span class="n">fields</span> <span class="o">=</span> <span class="n">zlines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">zv</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="n">fields</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;zv&#39;</span><span class="p">)</span><span class="o">+</span><span class="mi">2</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Now try newer style for OTF generated pspots</span>
                <span class="n">zlines</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;   | Element: &#39;</span><span class="p">),</span> <span class="n">pspot_lines</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">zlines</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Malformed pseudopotential file: does not contain pseudopotential report&#39;</span><span class="p">)</span>
                <span class="n">fields</span> <span class="o">=</span> <span class="n">zlines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">zv</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="n">fields</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;charge:&#39;</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">])</span>

            <span class="n">valence_charges</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="n">zv</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># It&#39;s an on-the-fly pseudopotential</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="n">pspot</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">)</span>
            <span class="n">valence_charges</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;unique elements and valence electrons: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">valence_charges</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">missing_pspots</span><span class="p">:</span>
        <span class="n">n_electrons</span> <span class="o">=</span> <span class="n">reduce</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> \
                             <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">el</span><span class="p">:</span> <span class="n">valence_charges</span><span class="p">[</span><span class="n">el</span><span class="p">]</span><span class="o">*</span><span class="nb">sum</span><span class="p">(</span><span class="n">cluster</span><span class="o">.</span><span class="n">species</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">==</span> <span class="n">el</span><span class="p">),</span> \
                                 <span class="n">elements</span><span class="p">))</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;total electrons </span><span class="si">%.1f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">n_electrons</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;spin_polarised&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;spin_polarised&#39;</span><span class="p">]</span> <span class="ow">and</span> \
            <span class="nb">int</span><span class="p">(</span><span class="n">n_electrons</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;spin polarised set to false but got odd number of electrons!&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s1">&#39;spin_polarised&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">param</span><span class="p">[</span><span class="s1">&#39;spin_polarised&#39;</span><span class="p">]</span> <span class="ow">and</span> \
            <span class="nb">int</span><span class="p">(</span><span class="n">n_electrons</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;spin polarised set to true but got even number of electrons!&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="run_castep"><a class="viewcode-back" href="../../io.html#quippy.castep.run_castep">[docs]</a><span class="k">def</span> <span class="nf">run_castep</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">stem</span><span class="p">,</span> <span class="n">castep</span><span class="p">,</span> <span class="n">castep_log</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">save_all_check_files</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">save_all_input_files</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">test_mode</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">copy_in_files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">subdir</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="s2">&quot;Invoke castep and return True if it completed successfully&quot;</span>

    <span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;castep_driver&#39;</span><span class="p">)</span>

    <span class="n">orig_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">subdir</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">subdir</span><span class="p">):</span>
                <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">subdir</span><span class="p">)</span>
            <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">subdir</span><span class="p">)</span>

        <span class="c1"># Write parameter file ...</span>
        <span class="n">param</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.param&#39;</span><span class="p">)</span>
        <span class="c1"># ... and cell file</span>
        <span class="n">cell</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.cell&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">copy_in_files</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">pspot</span> <span class="ow">in</span> <span class="n">copy_in_files</span><span class="p">:</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="n">pspot</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">(),</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">pspot</span><span class="p">)))</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">castep</span><span class="p">:</span> <span class="n">castep</span> <span class="o">=</span> <span class="n">castep</span> <span class="o">+</span> <span class="s1">&#39; </span><span class="si">%s</span><span class="s1">&#39;</span>

        <span class="k">if</span> <span class="n">test_mode</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;test mode: not running castep&#39;</span><span class="p">)</span>

            <span class="c1">## Simulate making check file</span>
            <span class="c1">#check_file = open(stem+&#39;.check&#39;,&#39;w&#39;)</span>
            <span class="c1">#check_file.close()</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Remove old output file and error files</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.castep&#39;</span><span class="p">)</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">*.err&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">):</span>
                <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

            <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">castep</span> <span class="o">%</span> <span class="n">stem</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">save_all_check_files</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.check&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">):</span>
                <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">while</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.check.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">)):</span>
                    <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.check&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.check.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">save_all_input_files</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.cell&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">):</span>
                <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">while</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.cell.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">)):</span>
                    <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.cell&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.cell.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.param&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">):</span>
                <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">while</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.param.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">)):</span>
                    <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.param&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.param.</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">))</span>

        <span class="n">err_files</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">*.err&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">)</span>
        <span class="n">got_error</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">err_files</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">err_files</span><span class="p">:</span>
                <span class="n">error_text</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">error_text</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                    <span class="n">got_error</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">error_text</span><span class="p">)</span>

        <span class="c1"># Write log file here so that it will always be written</span>
        <span class="k">if</span> <span class="n">castep_log</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.castep&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">):</span>
            <span class="n">logf</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">castep_log</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">)</span>
            <span class="n">castep_output</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.castep&#39;</span> <span class="o">%</span> <span class="n">stem</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
            <span class="n">logf</span><span class="o">.</span><span class="n">writelines</span><span class="p">(</span><span class="n">castep_output</span><span class="p">)</span>
            <span class="n">logf</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">finally</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">orig_dir</span><span class="p">)</span>

    <span class="k">return</span> <span class="ow">not</span> <span class="n">got_error</span></div>

<span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">Potential</span>

<div class="viewcode-block" id="CastepPotential"><a class="viewcode-back" href="../../io.html#quippy.castep.CastepPotential">[docs]</a><span class="k">class</span> <span class="nc">CastepPotential</span><span class="p">(</span><span class="n">Potential</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cell</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">param</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">castep_exec</span><span class="o">=</span><span class="s1">&#39;castep </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">stem</span><span class="o">=</span><span class="s1">&#39;castep_callback&#39;</span><span class="p">,</span> <span class="n">test_mode</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">little_clusters</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">copy_in_files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">subdir</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="n">Potential</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;CallbackPot little_clusters=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">({</span><span class="kc">True</span><span class="p">:</span> <span class="s1">&#39;T&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">:</span> <span class="s1">&#39;F&#39;</span><span class="p">}[</span><span class="n">little_clusters</span><span class="p">]))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_callback</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">run</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cell</span> <span class="o">=</span> <span class="n">CastepCell</span><span class="p">(</span><span class="n">cell</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cell</span> <span class="o">=</span> <span class="n">cell</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">param</span> <span class="o">=</span> <span class="n">CastepParam</span><span class="p">(</span><span class="n">param</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">param</span> <span class="o">=</span> <span class="n">param</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">castep_exec</span> <span class="o">=</span> <span class="n">castep_exec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stem</span> <span class="o">=</span> <span class="n">stem</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_mode</span> <span class="o">=</span> <span class="n">test_mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">copy_in_files</span> <span class="o">=</span> <span class="n">copy_in_files</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">subdir</span> <span class="o">=</span> <span class="n">subdir</span>

    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cell</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">cell</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cell</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cell</span> <span class="o">=</span> <span class="n">CastepCell</span><span class="p">()</span>
        <span class="n">cell</span><span class="o">.</span><span class="n">update_from_atoms</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">param</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">param</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">param</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">param</span> <span class="o">=</span> <span class="n">CastepParam</span><span class="p">()</span>
        <span class="n">param</span><span class="o">.</span><span class="n">update_from_atoms</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>

        <span class="n">stem</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stem</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.castep&#39;</span><span class="p">):</span>
            <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="n">stem</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%05d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stem</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
                <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.castep&#39;</span><span class="p">):</span> <span class="k">break</span>

        <span class="n">run_castep</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">stem</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">castep_exec</span><span class="p">,</span> <span class="n">test_mode</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">test_mode</span><span class="p">,</span> 
                   <span class="n">copy_in_files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">copy_in_files</span><span class="p">,</span> <span class="n">subdir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">subdir</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">test_mode</span><span class="p">:</span>
            <span class="nb">print</span> <span class="s1">&#39;Reading from file </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.castep&#39;</span><span class="p">)</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">stem</span><span class="o">+</span><span class="s1">&#39;.castep&#39;</span><span class="p">,</span> <span class="n">atoms_ref</span><span class="o">=</span><span class="n">at</span><span class="p">)</span>

            <span class="c1"># Update params -- this includes contents of .cell and .param templates</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">params</span><span class="p">)</span>

            <span class="c1"># Energy and force</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">energy</span><span class="p">)</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="n">result</span><span class="o">.</span><span class="n">force</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

            <span class="c1"># Virial stress tensor</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="s1">&#39;virial&#39;</span><span class="p">):</span>
                <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;virial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">virial</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;virial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>

            <span class="c1"># Add popn_calculate output and force components</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">properties</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;popn_&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;force_&#39;</span><span class="p">):</span>
                    <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">k</span><span class="p">),</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">print</span> <span class="s1">&#39;Test mode: setting energy, force and virial to zeros&#39;</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>
            <span class="n">at</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s1">&#39;force&#39;</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;virial&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span></div>



<div class="viewcode-block" id="read_formatted_potential"><a class="viewcode-back" href="../../io.html#quippy.castep.read_formatted_potential">[docs]</a><span class="k">def</span> <span class="nf">read_formatted_potential</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Load a potential write by CASTEP pot_write_formatted() routine, and convert</span>
<span class="sd">    to a 3-dimensional FortranArray suitable for writing to a .cube file.&quot;&quot;&quot;</span>

    <span class="c1"># Read header lines</span>
    <span class="n">header</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">fh</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;BEGIN header&#39;</span><span class="p">):</span>
        <span class="n">line</span> <span class="o">=</span> <span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;END header&#39;</span><span class="p">):</span>
            <span class="n">header</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">fh</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">fh</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="c1"># nsp_lines = [line for line in header if line.strip().endswith(&#39;! nsp&#39;) ]</span>
    <span class="c1"># if len(nsp_lines) != 1:</span>
    <span class="c1">#    raise IOError(&#39;Badly formatted potential file - missing &quot;nsp&quot; header line&#39;)</span>
    <span class="c1"># nsp = int(nsp_lines[0].split()[0])</span>

    <span class="c1"># num_ions_in_species_lines = [line for line in header if line.find(&#39;num_ions_in_species&#39;) != -1 ]</span>
    <span class="c1"># if len(num_ions_in_species_lines) != nsp:</span>
    <span class="c1">#    raise IOError(&#39;Badly formatted potential file - len(num_ions_in_species_lines) %d != nsp %d&#39; % (len(num_ions_in_species_lines), nsp))</span>
    <span class="c1"># num_ions_in_species = [ int(line.split()[0]) for line in num_ions_in_species_lines ]</span>

    <span class="c1"># # One line per ion with charge, potential at ion and efield at ion</span>
    <span class="c1"># pot_ions = []</span>
    <span class="c1"># efield_ions = []</span>
    <span class="c1"># for sp in range(nsp):</span>
    <span class="c1">#    for i in range(num_ions_in_species[sp]):</span>
    <span class="c1">#       line = fh.readline()</span>
    <span class="c1">#       species, index, charge, potential, efield1, efield2, efield3 = line.split()</span>
    <span class="c1">#       pot_ions.append(float(potential))</span>
    <span class="c1">#       efield_ions.append([float(efield1), float(efield2), float(efield3)])</span>

    <span class="c1"># pot_ions = farray(pot_ions)</span>
    <span class="c1"># efield_ions = farray(efield_ions).T</span>

    <span class="c1"># Rest of file is potential data grid</span>
    <span class="n">pot</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">fh</span><span class="p">)</span>
    <span class="n">nx</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span> <span class="o">=</span> <span class="n">pot</span><span class="p">[:,</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span> <span class="n">pot</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span> <span class="n">pot</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="n">nx</span><span class="p">,</span><span class="n">ny</span><span class="p">,</span><span class="n">nz</span><span class="p">))</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">value</span><span class="p">)</span> <span class="ow">in</span> <span class="n">pot</span><span class="p">:</span>
        <span class="n">data</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="nb">int</span><span class="p">(</span><span class="n">j</span><span class="p">),</span><span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">)]</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">return</span> <span class="n">data</span></div>

<div class="viewcode-block" id="read_formatted_density"><a class="viewcode-back" href="../../io.html#quippy.castep.read_formatted_density">[docs]</a><span class="k">def</span> <span class="nf">read_formatted_density</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Load a potential write by CASTEP pot_write_formatted() routine, and convert</span>
<span class="sd">    to a 3-dimensional FortranArray suitable for writing to a .cube file.&quot;&quot;&quot;</span>

    <span class="n">den</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">loadtxt</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span>
    <span class="n">nx</span><span class="p">,</span> <span class="n">ny</span><span class="p">,</span> <span class="n">nz</span> <span class="o">=</span> <span class="n">den</span><span class="p">[:,</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span> <span class="n">den</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span> <span class="n">den</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="n">nx</span><span class="p">,</span><span class="n">ny</span><span class="p">,</span><span class="n">nz</span><span class="p">))</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">charge</span><span class="p">,</span><span class="n">spin</span><span class="p">)</span> <span class="ow">in</span> <span class="n">den</span><span class="p">:</span>
        <span class="n">data</span><span class="p">[:,</span><span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="nb">int</span><span class="p">(</span><span class="n">j</span><span class="p">),</span><span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">)]</span> <span class="o">=</span> <span class="n">charge</span><span class="p">,</span> <span class="n">spin</span>
    <span class="k">return</span> <span class="n">data</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2008-2016, James Kermode

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>